为什么需要 $ = jQuery
问题几乎是理论上的。但上次我遇到了一些微妙的错误。当页面包含子框架并且父框架和子框架都使用相同的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果页面中的任何位置包含其他 JavaScript 库,它们可能会使用
$
。 Mootools 和 原型< /a> 都使用$
。如果您包含 jQuery,然后包含 Prototype,则
$
变量将被覆盖,因此您必须在任何 jQuery 代码运行之前设置$ = jQuery
:同样,正如 @Guffa 所说,如果您在任何地方调用 noConflict ,它将取消设置
$
多变的。理想情况下,我认为您不应该使用
$
变量,因为它可能会导致问题。只需执行以下操作:然后使用
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: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:And then use the
jQ
variable as you would have the$
. Then you won't get conflicts.除非您从
$
变量中删除了引用,否则不需要这样做。检查您是否在某处使用 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$
.