如何找到最接近的事件元素
我正在使用jQuery。 我想找到与创建事件的元素最接近的类.hidebox
。 我试图使用parent
, find
和closest
但它们都引用了我的元素所在的级别。 我只是想找到我能找到的最接近我的event元素的.hidebox
类。
| show div |
| show div |
$(".hideBox-tab").click(function(){ $(this) .parent().parent() .find(".hideBox") .toggle(); return false; });
正如Rory McCrossan所说; 您的第一个hideBox
直接出现在
标签中,该标签无效; 第二个hideBox
的
|
出现在
之外,也是无效的。 所以我使用的HTML下面与你的有些不同,但我相信你只想要那个
HTML:
| show div | |
| show div | |
JQuery代码:
$(".hideBox-tab").click(function(){ $(this).closest("tr").find(".hideBox").toggle(); return false; });
尽管您的HTML有效性,您可以尝试这样做:
$(".hideBox-tab").click(function(){ $(this) .closest("tr") .next("tr") .find(".hideBox") .toggle(); return false; });
.hideBox { display:none; }