所以这是我当前的 jquery 代码,我希望在 vanilla js 中使用它。
var elems = [];
$("*").not('script, style, iframe').each(function() {
elems.push($(this)[0]);
});
我找到的最接近的替代方案是
document.getElementsByTagName("*")
但它仍然有 iframe、样式和脚本标记,我不希望它们出现在我的数组中。
另外,我不能只通过它们的标签名称来删除它们,因为它们可能有一个 ID 或一个与之关联的类。
最佳答案
一种选择是将 CSS 选择器与 .querySelectorAll()
method 一起使用.
因为 CSS 有一个 :not()
pseudo-class ,您可以使用它来否定 script
、style
和 iframe
元素。
var elements = document.querySelectorAll('*:not(script):not(style):not(iframe');
在上面的代码片段中,elements
将是一个 NodeList
包含所选元素的集合。
关于javascript - jquery 到 Vanilla js; html标签的每个循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41915959/