简体   繁体   English

在 DOM 中遍历跟随轴

[英]Traverse the following axis in the DOM

This is a DOM fragment.这是一个 DOM 片段。

<p class="target">1</ p>
<p class="click"></ p>
<table>
<tr><td><p class="click"></p></td></tr>
</table>
<p class="target">2</p>

I would like to find the next p with the target class, it doesn't matter on what DOM level the p pressed with the click class is.我想找到target类的下一个 p,与click类按下的 p 是什么 DOM 级别无关。 In the case p with value 2在 p 值为 2 的情况下

Something like就像是

$ ("p.click").click(function () {
target = $(this).following("p.target").first()
});

But I can not find how to traverse the following axis in the DOM, except, maybe, xpath which does not work in all browsers.但是我找不到如何在 DOM 中遍历跟随轴,除了 xpath 可能不适用于所有浏览器。

It would be great if I don't know something :)如果我什么都不知道那就太好了:)

EDIT编辑

I was not correct enough writing the question.我写这个问题不够正确。 More suitable code is below.更合适的代码如下。 After pressing click I want to get the first error afterclick我想得到第一个error

<input class="click" type="button" value="1">
<p class="error"></ p>
<table>
<tr><td><input class="click" type="button" value="2"></td></tr>
</table>
<p class="error"></p>

By retrieving all p s initially, you can check the .index of the clicked element ( this ) in the collection.通过检索所有p s ^首先,你可以检查.index单击元素(的this集合中)。 Then, access index + 1 in that p collection:然后,访问该p集合中的index + 1

 const $ps = $('p'); $("p.click").click(function() { const thisPIndex = $ps.index(this); console.log($($ps[thisPIndex + 1]).text()); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p class="target">1</ p> <p class="click"></ p> <table> <tr> <td> <p class="click">click</p> </td> </tr> </table> <p class="target">2</p>

If the clicked element is not necessarily one of the <p> s in the collection whose index you care about, you can follow a similar method by constructing a collection of all elements in the HTML, finding the index of the clicked element in the collection, constructing a new collection starting from that index + 1 , filter ing by p , and checking the first matching element:如果被点击的元素不一定是你关心索引的集合中的<p>之一,你可以按照类似的方法构造一个 HTML 中所有元素的集合,在集合中找到被点击元素的索引,从该index + 1开始构造一个新集合,通过p filter ,并检查第一个匹配元素:

 const $ps = $('p'); const allElms = $('*'); $(".click").click(function() { const thisElmIndex = allElms.index(this); const followingElements = allElms.slice(thisElmIndex + 1); console.log(followingElements.filter('p')[0].textContent); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input class="click" type="button" value="1"> <p class="error">1</p> <table> <tr> <td><input class="click" type="button" value="2"></td> </tr> </table> <p class="error">2</p>

.index() & .eq() .index() & .eq()

Assuming that there's an .error for each .click , we can:假设每个.click都有一个.error ,我们可以:

  • listen for a click on any .click听点击任何.click
  • find the clicked .click index number relating to all .click on the document找到与所有.click相关的已点击.click索引号
  • then use that index number to find the equivalent index number among the .error on the document.然后使用该索引号在文档的.error找到等效的索引号。


Demo演示

Details commented in demo演示中评论的详细信息

 /* - Register click event on document - Any .click clicked will be $(this) - Get the index number of the clicked button in relation to all .click on the document. - Use the same index number to find the associated .error */ $(document).on('click', '.click', function(e) { var idx = $(this).index('.click'); $('.error').eq(idx).show(); });
 .error { display: none }
 <input class="click" type="button" value="1"> <p class="error">ERROR 1</ p> <table> <tr> <td><input class="click" type="button" value="2"></td> </tr> </table> <p class="error">ERROR 2</p> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

$ ("p.click").click(function () {
target = $(this).nextAll("p.target").first() // following-->nextAll
});

Here is your playground这里是你的游乐场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM