admin管理员组文章数量:1424887
I've got some code that looks like this:
jQuery(this).parent().parent().parent().parent().next(".results-table").css("display", "block");
Basically, after clicking a link inside of a table, the following results table will show, the problem is that this link is inside a td, which is inside a tr, which of course is inside a table, hence the repeated parent selector.
Is there a better way of doing this that I'm missing? I've not got lots of experience with jQuery unfortunately.
I've got some code that looks like this:
jQuery(this).parent().parent().parent().parent().next(".results-table").css("display", "block");
Basically, after clicking a link inside of a table, the following results table will show, the problem is that this link is inside a td, which is inside a tr, which of course is inside a table, hence the repeated parent selector.
Is there a better way of doing this that I'm missing? I've not got lots of experience with jQuery unfortunately.
Share Improve this question asked Sep 20, 2012 at 12:30 SeanSean 6,5099 gold badges49 silver badges70 bronze badges 2- 2 depending on your DOM, .parents("yourSelector") might work better – anderssonola Commented Sep 20, 2012 at 12:32
- Could you add an example block of your table showing any classes or id's on elements? That would be more beneficial than explaining how your elements are layed out. – Jeff LaFay Commented Sep 20, 2012 at 12:33
5 Answers
Reset to default 6You can use jQuery(this).closest("table")
http://api.jquery./closest/
jQuery(this).parents("table").next(".results-table").css("display", "block");
or
jQuery(this).closest("table").next(".results-table").css("display", "block");
jQuery(this).closest('table').css("display", "block");
or even
jQuery(this).closest('.results_table').css("display", "block");
dependent on your desired result and your HTML elements.
$(this).parents(":eq(3)")
This could be helpful
Or.. you could possibly make use of event delegation, such as .on() instead of .click(). Then, you can simply use e.delegateTarget.
For example,
$(parent).on("click", link, function(e) {
jQuery(e.delegateTarget).next(".results-table").css("display", "block");
});
本文标签: javascriptUsing parent() three timesis there a better wayStack Overflow
版权声明:本文标题:javascript - Using parent() three times, is there a better way? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745405082a2657214.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论