这两个选择器之间有什么区别?
$(this).parents('table:first > tbody > tr')
和
$(this).parents('table:first').children('tbody').children('tr')
区别在于第一个选择器完全在parents
调用内,而第二个选择器不在parents
调用内。
因此,第一个查找匹配table:first > tbody > tr
所有父项table:first > tbody > tr
。 (换句话说,包含this
在第一个table
的tr
)
第二个将找到匹配table:first
的父项table:first
,然后直接在该父项的tbody
s中查找所有tr
。 (换句话说,直接在父表中的所有tr
)
也许一个例子将有助于……从这个HTML开始
Outter Table 1 Inner Table 2a 2b 2c
3
应用此脚本:
$('.test').parents('table:first > tbody > tr').addClass('foo'); $('.test').parents('table:first').children('tbody').children('tr').addClass('bar');
结果:
Outter Table 1 Inner Table 2a 2b 2c
3