0

現在、データベースの結果からの while ループであるテーブルがあります。私の表の行には、+ または - の日数になる値が含まれています。td に ("-") または ("+") が含まれているかどうかを調べる jQuery スクリプトを作成しようとしています。その行の最初の td に css スタイルを適用します。現時点では、すべての行の最初の td ごとに適用されます。

        $("tr td:nth-child(7):contains('+')").each(function() {
    $('tr td:first-child').css('background-color', 'blue');
    });
4

2 に答える 2

2

$(this)ループ内で「this」要素を参照するように使用する必要があります。

次に、「this」がtd要素であるため、 parentを見つけて、その要素内にネストされた をtr見つけます。first-child

$("tr td:nth-child(7):contains('+')").each(function() {
    $(this).parent("tr").find('td:first-child').css('background-color', 'blue');
});

デモを参照してください: http://jsfiddle.net/g7ZTf/

于 2012-08-06T10:02:15.723 に答える
1

私はこのようにします...

$(function(){
    $("tr td:nth-child(7):contains('+')").each(function() {
        // get a jQuery object of the selected element using `$(this)`
        // then select `.siblings()`, and limit to first element in array (`.eq(0)`)
        $(this).siblings().eq(0).css('background-color', 'blue');
    });
});​

フィドル: http://jsfiddle.net/FqUB3/

于 2012-08-06T10:07:47.903 に答える