2 回答

TA貢獻1802條經(jīng)驗 獲得超4個贊
console.log(document.querySelector('a~c+b~c').innerText)
<a></a>
<c>incorrect</c>
<b></b>
<c>correct</c>
<b></b>
<a></a>
console.log(document.querySelector('d~c+a~c').innerText)
<a></a>
<b> </b>
<c> </c>
<c>incorrect</c>
<a></a>
<c>incorrect</c>
<b> </b>
<d> </d>
<b> </b>
<a></a>
<c>incorrect</c>
<a></a>
<c>correct</c>
<a></a>
<c>incorrect</c>
<d> </d>

TA貢獻1829條經(jīng)驗 獲得超7個贊
要選擇<c>correct</c>,您可以使用:
document.querySelector('b+c');
這將選擇所有c直接跟在 a 之后的內(nèi)容b。
如果你想選擇位于兩個元素之間的所有元素,你不能為此使用 CSS。
雖然有一點點 javascript 是可能的:
// selects all 'c' following an 'a'
for (const element of document.querySelectoAll("a+b")) {
// checks if the next element after 'c' is 'b'
if (element.nextSibling().tagName == "b") {
// prints every 'c' that follows an 'a' and has a 'b' after
console.log(element);
}
}
添加回答
舉報