我在没有 jQuery(或任何其他使用 $
符号作为快捷方式的库)的页面上使用 google chrome 的开发人员工具。
当我通过控制台检查 $
时(只需输入并按 Enter 键),我得到了:
$
function () { [native code] }
所以,chrome有一些原生函数可以通过$
引用。只有 chrome 似乎有这个,我无法通过 window['$']
或通过 document['$']
或 this['$'] 访问它]
。
我无法找出这个功能是什么。您知道它的作用吗?也许有一些相关的背景信息? 提前致谢!
最佳答案
即使从去年开始,情况又发生了变化。
devtools 控制台提供 $
作为 document.querySelector
的别名,以及 many other things ;以下是摘录列表:
$(selector)
returns the reference to the first DOM element with the specified CSS selector. This function is an alias for thedocument.querySelector()
function.$$(selector)
returns an array of elements that match the given CSS selector. This command is equivalent to callingdocument.querySelectorAll()
.$_
returns the value of the most recently evaluated expression.- The
$0
,$1
,$2
,$3
and$4
commands work as a historical reference to the last five DOM elements inspected within the Elements panel or the last five JavaScript heap objects selected in the Profiles panel.
...还有其他一些。
请注意它如何调用 $
document.querySelector
的别名,但表示 $$
与调用 document “等效” .querySelectorAll
。从字面上看,这两种情况似乎都不是真的。 $ === document.querySelector
为 false
,并且 $$
返回一个 array,而不是 节点列表
.
关于javascript - Chrome 中的 $ 变量(美元符号)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47290639/