为什么需要 $ = jQuery

发布于 2024-11-29 21:24:37 字数 119 浏览 5 评论 0原文

问题几乎是理论上的。但上次我遇到了一些微妙的错误。当页面包含子框架并且父框架和子框架都使用相同的js文件时,如果我没有在开始时设置$ = jQuery,则该js文件的功能会崩溃。这里到底有什么窍门呢?

谢谢!

Question is nearly theoretical. But last time I've encountred some subtle bug. When the page contains child frame and both parent and child frame are used the same js file, if I not set $ = jQuery on the start, functionality of this js file crashed. What the trick could be here?

Thanks!

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

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

发布评论

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

评论(2

你对谁都笑 2024-12-06 21:24:37

如果页面中的任何位置包含其他 JavaScript 库,它们可能会使用 $Mootools原型< /a> 都使用 $

如果您包含 jQuery,然后包含 Prototype,则 $ 变量将被覆盖,因此您必须在任何 jQuery 代码运行之前设置 $ = jQuery

<link rel="stylesheet" href="jQuery.js" />
<link rel="stylesheet" href="prototype.js" />
<script>$ = jQuery</script>

同样,正如 @Guffa 所说,如果您在任何地方调用 noConflict ,它将取消设置 $ 多变的。

理想情况下,我认为您不应该使用 $ 变量,因为它可能会导致问题。只需执行以下操作:

jQuery.noConflict();
var jQ = jQuery;

然后使用 jQ 变量,就像使用 $ 一样。那么你们就不会发生冲突。

If there are other JavaScript libraries included anywhere in the page, they might use $. Mootools and Prototype both use $.

If you include jQuery and then you include Prototype then the $ variable will be over-written, so you'd have to set $ = jQuery before any jQuery code would work:

<link rel="stylesheet" href="jQuery.js" />
<link rel="stylesheet" href="prototype.js" />
<script>$ = jQuery</script>

Equally, as @Guffa says, if you called noConflict anywhere it would unset the $ variable.

Ideally, I don't think you should use the $ variable, as it can cause problems. Just do something like:

jQuery.noConflict();
var jQ = jQuery;

And then use the jQ variable as you would have the $. Then you won't get conflicts.

灰色世界里的红玫瑰 2024-12-06 21:24:37

除非您从 $ 变量中删除了引用,否则不需要这样做。

检查您是否在某处使用 noConflict 方法来将 jQuery 与 $ 变量,或者有其他名为 $ 的东西(如函数)。

That should not be needed unless you removed the reference from the $ variable.

Check if you are using the noConflict method somewhere to decouple jQuery from the $ variable, or have something else (like a function) named $.

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