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