jQuery、Prototype 和插件之间的冲突
我正在尝试在 Rails 应用程序中消除 Prototype 和 jQuery 的冲突。我发现 jQuery 将 Prototype 很好地融入到了应用程序中。
我通过将 $jq = jQuery.noConflict();
添加到 application.js 文件来开始该过程。
然后,我开始检查项目中的所有 .erb 和 .js 文件,查找对 $
的 jQuery 调用,并将其替换为 $jq
。然后我找到了一些 jQuery 插件文件。
我是否必须将 jQuery 插件 .js 文件中的所有 $
引用转换为 $jq
?
I am trying to deconflict Prototype and jQuery in a rails app. I discovered that jQuery broke Prototype a good way into the app.
I started the process by adding $jq = jQuery.noConflict();
to the application.js file.
I then began going through all the .erb and .js files in the project, looking for jQuery calls to $
and replacing them with $jq
. Then I got to some of the jQuery plugin files.
Do I have to convert all $
references to $jq
in the jQuery plugin .js files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我今天遇到了这个问题,包装插件代码如下:
直到我将包含原型库文件的行移到包含 jQuery 库文件的行之前才起作用。
I came across this problem today and wrapping plugin code like:
didn't work until I moved the line which includes prototype library files before the line which includes jQuery library files.
一般来说,不会。大多数插件设置为将 jQuery 对象作为 $ 传递。您可以判断插件是否执行此操作,因为它看起来像这样:
Generally, no. Most plugins are set to pass the jQuery object in as $. You can tell if a plugin does this, because it'll look like this:
您是否考虑过将其添加到原型库js文件中?这样,无论何时加载原型,它都会自动消除 jquery 的冲突。
我们用 mootools 做了类似的事情。
在回答您的问题时,如果插件已正确编写,那么您不必在那里进行更改。以下约定应该包装插件,确保即使在打开 noconflict 时也能正确运行。
Have you considered adding it to the prototype library js file? That way, whenever prototype is loaded it automatically deconflicts jquery.
We have done a similar thing here with mootools.
In answer to your question, if the plugin has been written correctly, then you shouldn't have to change this there. The following convention should wrap the plugin, ensuring correct operation even when noconflict has been turned on.
或者你可以将它们包裹在一个封闭物中
Or you could wrap them in a closure
尝试按顺序一起加载 jQuery 及其所有插件,然后调用 noConflict()。这是一个示例:
然后,稍后在您自己的代码中,您可以使用 $jq.jQuery 变量引用 jQ —— 包含您想要的插件。
Try loading jQuery and all its plugins together, in a sequence, and then call noConflict(). Here's an example:
Then later in your own code you can reference jQ -- complete with the plugins you desire -- using the $jq.jQuery variable.