Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JQuery仅添加到奇偶索引的子级_Jquery - Fatal编程技术网

JQuery仅添加到奇偶索引的子级

JQuery仅添加到奇偶索引的子级,jquery,Jquery,我有以下JQuery: $(document).ready(function () { $('tr').hover(function () { $(this).children().addClass("color2"); $(this).children().removeClass("color1"); }); }); 但是我只想把它添加到一个索引为偶数(包括0)的td元素上 将$(this)替换为$('td:even',

我有以下JQuery:

$(document).ready(function () {
    $('tr').hover(function () {            
        $(this).children().addClass("color2");
        $(this).children().removeClass("color1");
    });
});

但是我只想把它添加到一个索引为偶数(包括0)的
td
元素上

$(this)
替换为
$('td:even',this)

编辑:

我第一次没有看到
children()
。因此,将
$(this).children()
替换为
$('td:even',this)
关于
children()
函数可以有一个过滤器参数。在您的情况下,您只希望选择偶数的(td)子项。所以在
children('td:偶数')

$('tr > td:even').hover(function() {
  // ...
});