这段代码在我正在学习的教程中,它按照DOM中的顺序记录页面上的所有标题标记。有人能解释一下这段代码是如何工作的吗?(特别是headings = $$('h1,h2...')
部分)
$$字符表示什么?
for (var i = 0, headings = $$('h1,h2,h3,h4,h5,h6');
i < headings.length; i++) {
console.log(headings[i].textContent.trim() + " " +
headings[i].tagName,
headings[i]);
}
发布于 2019-04-08 06:54:46
正如在注释中提到的,它可能是document.querySelectorAll()
或其他dom查询方法的包装器函数。
function $$(selectors) {
return document.querySelectorAll(selectors)
}
for (var i = 0, headings = $$('h1,h2,h3,h4,h5,h6'); i < headings.length; i++) {
console.log(headings[i].textContent.trim() + " " +
headings[i].tagName,
headings[i]);
}
<h1>One</h1>
<h2>Two</h2>
发布于 2019-04-08 08:09:07
我想通了。它是一个控制台实用程序API便利功能,只能从控制台运行。这个$$基本上是querySelectorAll的内置控制台快捷方式。https://developers.google.com/web/tools/chrome-devtools/console/utilities
https://stackoverflow.com/questions/55564326
复制相似问题