我有一个表,其中可能包含其他内部表(无法编辑生成的标记)。我想为 mouseenter 和 mouseleave 行创建一个委托(delegate)函数,它仅触发关联的主表行(而不是内表行),如下所示:
$("#tableid").delegate("tr", "mouseenter mouseleave", function(e) {
//do stuff here
});
但是使用此选择器它还会选择内部表格行,那么如何修改选择器以避免选择内部表格行?
最佳答案
您可以使用 .delegate()
使用稍微不同的选择器,如下所示:
$("#tableid").delegate("#tableid > tbody> tr", "mouseenter mouseleave", function(e) {
//do stuff, for example $(this).toggleClass("hover");
});
这有效,因为你想要 <tr>
元素是直接子行,但有一个 <tbody>
您需要考虑两者之间的情况。 You can test a working demo here .
关于jQuery 委托(delegate)表行选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2942204/