即使没有JQuery,JavaScript中的美元符号是否也定义?

发布于 2025-02-04 13:44:39 字数 1263 浏览 4 评论 0原文

我知道$(“ ...”)当然是在jquery中定义的( javaScript 中的“ $”符号的含义是什么,而且有些不使用jQuery的人define $ = document.queryselector$ = document.queryselector.bind(document)

在某些情况下(Firefox,Chrome),$似乎也可以在开发人员控制台中使用,即使不加载此库,也没有手动定义$:请参阅<<。 a href =“ https://stackoverflow.com/questions/222244823/what-is-the-dollar-sign-in-sign-in-javascript-if-not-jquery”> javascript中的美元符号是什么,如果不是jquery 。

问题:我们可以在2022年假设$是大多数浏览器的别名document.queryselector,我们可以直接在JS代码中使用它吗?

我知道caniuse.com,但这里都不 https://caniuse.com/?search=%24 nor https://caniuse.com/?search=dollar 帮助了。

示例:打开一个包含的HTML文件:

<html>
<head></head>
<body>
<div id="a">Hello world</div>
</body>
</html>

打开开发人员控制台并执行$('#a'):它有效。

I know that $("...") is of course defined in jQuery (What is the meaning of "$" sign in JavaScript), and also that some people who don't use jQuery define $ = document.querySelector or $ = document.querySelector.bind(document).

In some cases (Firefox, Chrome), $ seems to be available in the developer console even without loading this library, or without defining $ manually: see the main answer of What is the dollar sign in Javascript, if not jQuery.

Question: can we assume in 2022 that $ is an alias in most browsers for document.querySelector and can we use it directly in our JS code?

I know caniuse.com but here neither https://caniuse.com/?search=%24 nor https://caniuse.com/?search=dollar helped.

Example: open a html file containing:

<html>
<head></head>
<body>
<div id="a">Hello world</div>
</body>
</html>

Open the developer console and do $('#a'): it works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぺ禁宫浮华殁 2025-02-11 13:44:39

不,你不能。它是仅在某些浏览器中的开发人员控制台中可用的,而无需导入jquery库。

参见 https:// https://developer.chrome.chrome.com/docs/docs/devtools/devtools/devtools/控制台/实用程序/#queryselector-function

$(选择器)用指定的CSS选择器返回对第一个DOM元素的引用。当使用一个参数调用时,此函数是document.queryselector()函数的别名。

这是一个简单的例子:

console.log($('#div'));
<div id="div"></div>

No you can't. It's only available in developer console in some browsers without importing jQuery library.

See https://developer.chrome.com/docs/devtools/console/utilities/#querySelector-function:

$(selector) returns the reference to the first DOM element with the specified CSS selector. When called with one argument, this function is an alias for the document.querySelector() function.

Here is a simple example:

console.log($('#div'));
<div id="div"></div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文