如何从具有一个到多个 class 名称的元素中找到具有完全特定且唯一的 class 名称(而不是任何其他名称)的元素?
How to find from elements with one to multiple class names just the ones which feature exactly a specific and sole class name (and not any other)?
示例:
<div class="parent father"> ... </div>
<div class="parent"> ... </div>
const soleParentClassElementList = document.querySelectorAll('.parent')
使用上面的代码行,我会查询两个元素节点,但我只想查询一个 parent
class.
这样...
document
.querySelectorAll('[class="parent"]')
.forEach(node => node.textContent = 'this one');
<div class="parent father"> ... </div>
<div class="parent"> ... </div>
示例:
<div class="parent father"> ... </div>
<div class="parent"> ... </div>
const soleParentClassElementList = document.querySelectorAll('.parent')
使用上面的代码行,我会查询两个元素节点,但我只想查询一个 parent
class.
这样...
document
.querySelectorAll('[class="parent"]')
.forEach(node => node.textContent = 'this one');
<div class="parent father"> ... </div>
<div class="parent"> ... </div>