admin管理员组文章数量:1356241
In CSS, if you want to target a specific element that immediately follows another specific element, you would use the next-sibling combinator:
<style>
/* any 'p' that immediately follows 'div' will be red */
div + p { color: red; }
</style>
<div>
<p>foo</p> <!-- black -->
</div>
<p>bar</p> <!-- red -->
<p>baz</p> <!-- black -->
<div>
<p>foo</p> <!-- black -->
</div>
<p>bar</p> <!-- red -->
<p>baz</p> <!-- black -->
But how to make it in JavaScript? I tried closest
, but I'm not surprised it doesn't work. If I understand its description on MDN, it is intended to work differently.
<div class="input" style="display: none">
foo
</div>
<div class="output"></div>
<div class="input" style="display: none">
foo
</div>
<div class="output"></div>
<script>
'use strict';
window.onload = function() {
for (const input of document.querySelectorAll('.input')) {
document.querySelector(input.closest('body > .output')).innerHTML = 'input.innerHTML';
}
}
</script>
How to make it work?
In CSS, if you want to target a specific element that immediately follows another specific element, you would use the next-sibling combinator:
<style>
/* any 'p' that immediately follows 'div' will be red */
div + p { color: red; }
</style>
<div>
<p>foo</p> <!-- black -->
</div>
<p>bar</p> <!-- red -->
<p>baz</p> <!-- black -->
<div>
<p>foo</p> <!-- black -->
</div>
<p>bar</p> <!-- red -->
<p>baz</p> <!-- black -->
But how to make it in JavaScript? I tried closest
, but I'm not surprised it doesn't work. If I understand its description on MDN, it is intended to work differently.
<div class="input" style="display: none">
foo
</div>
<div class="output"></div>
<div class="input" style="display: none">
foo
</div>
<div class="output"></div>
<script>
'use strict';
window.onload = function() {
for (const input of document.querySelectorAll('.input')) {
document.querySelector(input.closest('body > .output')).innerHTML = 'input.innerHTML';
}
}
</script>
How to make it work?
Share Improve this question asked Mar 30 at 10:53 jsx97jsx97 1071 silver badge5 bronze badges 6 | Show 1 more comment2 Answers
Reset to default 2This is covered by the second flagged duplicate, Is there a way to select sibling nodes?, and basically copied from the nextElementSibling documentation but I'll put it here for now.
Iterate over each input
's nextElementSibling
s until you find one that contains the relevant class. Here using classList.contains
for (const input of document.querySelectorAll('.input')) {
let el = input.nextElementSibling;
while (el && !el.classList.contains("output")) {
el = el.nextElementSibling;
}
if (el) {
el.innerHTML = input.innerHTML;
}
}
<div class="input" style="display: none">
foo-1
</div>
<h3>Foo 1</h3>
<div class="output"></div>
<div class="input" style="display: none">
foo-2
</div>
<h3>Foo 2</h3>
<div class="output"></div>
You can use Element#nextElementSibling
:
<div class="input" style="display: none">
foo
</div>
<div class="output"></div>
<div class="input" style="display: none">
foo
</div>
<div class="output"></div>
<script>
'use strict';
window.onload = function() {
for (const input of document.querySelectorAll('.input')) {
input.nextElementSibling.innerHTML = input.innerHTML;
}
}
</script>
本文标签: javascriptFind specific element that follows another specific elementStack Overflow
版权声明:本文标题:javascript - Find specific element that follows another specific element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743990982a2572130.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
querySelectorAll()
? eg:querySelectorAll('.input + .output')
? – Nick Parsons Commented Mar 30 at 11:02