简体   繁体   English

如何用 JS 选择某种类型的下一个兄弟?

[英]How to select next sibling of a certain type with JS?

I've got some html我有一些html

<h4 id="start-here">title</h4>
<p>paragraph</p>
<p>paragraph</p>
...some number of paragraphs... 
<a href="#" class="link">link</a>

And I've got the <h4> with the id selected in JavaScript.而且我有<h4>在 JavaScript 中选择了 id。 How do I get from that selection in JS to the first <a> which is of the class link, or just the next sibling anchor tag?我如何从 JS 中的选择到第一个<a>类链接,或者只是下一个兄弟锚标记?

Using document.querySelector() and a CSS selector, here with the general sibling combinator ~ , you can achieve that like this:使用document.querySelector()和 CSS 选择器,这里使用通用兄弟组合~ ,您可以像这样实现:

A side note, in below samples I target inline style, though it is in general better to toggle a class.附带说明,在下面的示例中,我以内联样式为目标,尽管通常切换类更好。

Stack snippet堆栈片段

 (function(){ document.querySelector('#start-here ~ a.link').style.color = 'red'; })();
 <h4 id="start-here">title</h4> <p>paragraph</p> <a href="#">link</a> <p>paragraph</p> <a href="#" class="link">link</a>


Updated based on another question/comment, how to get more than one element in return.根据另一个问题/评论更新,如何获得多个元素作为回报。

With document.querySelectorAll() one can do similar, and target multiple elements like this.使用document.querySelectorAll()可以做类似的事情,并像这样定位多个元素。

Stack snippet堆栈片段

 (function(){ var elements = document.querySelectorAll('#div2, #div3'); for (var i = 0; i < elements.length; i++) { elements[i].style.color = 'red'; } })();
 <h4 id="start-here1">title</h4> <div id="div1">some text</div> <h4 id="start-here2">title</h4> <div id="div2">some text</div> <h4 id="start-here3">title</h4> <div id="div3">some text</div>

The "start-here" ID on your element makes this easy.元素上的“start-here”ID 使这很容易。 But let's imagine you have a reference to a DOM element without such a convenient selector, and you don't want to add a temporary ID to it.但是让我们假设您有一个没有如此方便的选择器的 DOM 元素的引用,并且您不想给它添加一个临时 ID。

In that case, you could use XPath with document.evaluate and your DOM reference as the second argument.在这种情况下,您可以将 XPath 与document.evaluate一起使用,并将您的 DOM 引用作为第二个参数。 Let's say you have that reference in yourElement and you want the next <section> sibling假设您在yourElement中有该引用,并且您想要下一个<section>兄弟

const nextSibling = document.evaluate("following-sibling::section", yourElement, null,
  XPathResult.FIRST_ORDERED_NODE_TYPE).singleNodeValue

I think to start with the first sibling, then i put all the siblings inside an array.我想从第一个兄弟姐妹开始,然后我将所有兄弟姐妹放在一个数组中。 Hence I extract what you want.因此我提取你想要的。

 var x = document.getElementById("stat-here"); console.log(x) var result = [], node = x.nextSibling; while ( node ) { if (node.nodeType === Node.ELEMENT_NODE ) { result.push( node ); } node = node.nextElementSibling || node.nextSibling; } console.log(result, '\n Result: ',result[result.length-2])
 <h4 id="stat-here">title</h4> <p>paragraph</p> <p>paragraph</p> <a href="#" class="link">link</a>

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

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