我正在使用一个脚本,我想选择网页的所有节点
nodes = document.body.getElementsByTagName('*');
Run Code Online (Sandbox Code Playgroud)
但我想排除一个元素,比如#ThisID,以及它的所有子元素
我怎么做?
你可以用querySelectorAll,
nodes = document.body.querySelectorAll('*:not(#ThisID)');
Run Code Online (Sandbox Code Playgroud)
* 将选择身体中的所有元素.:not(selector) 将过滤掉与提供的选择器相关的元素.为了排除需要过滤的节点的子节点,
var nodes = Array.from(document.body.querySelectorAll('*:not(#ThisID)'));
var filter = document.querySelector("#ThisID");
nodes = nodes.filter(function(node) {
return !filter.contains(node);
});
console.log(nodes); // This will not contain the node #test1 as well its children.
Run Code Online (Sandbox Code Playgroud)
迭代收集的节点并使用过滤掉所需的节点 .contains
| 归档时间: |
|
| 查看次数: |
36 次 |
| 最近记录: |