如何使用 jquery.noConflict 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1391950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use jquery.noConflict property
提问by josh
I am testing my webpage on a server using preview dns. Just realized that preview dns automatically adds mootools library towards the end of any php page. Probably for their own statistics or something.
我正在使用预览 dns 在服务器上测试我的网页。刚刚意识到预览 dns 会在任何 php 页面的末尾自动添加 mootools 库。大概是为了自己的统计什么的。
But the problem is that I am using jquery in my page. So My jquery code breaks because both mootools and jquery both use '$'.
但问题是我在我的页面中使用了 jquery。所以我的 jquery 代码中断了,因为 mootools 和 jquery 都使用 '$'。
I've put all the page source of my page on jsbin: http://jsbin.com/ozime(this includes the added on mootools).
我已将我页面的所有页面源代码放在 jsbin 上:http://jsbin.com/ozime (这包括在 mootools 上添加的)。
In this page I've added a sample jquery code which should trigger on change of the drop down box. I added some alert statements as well. However, only first alert statement shows up. One inside jquery code does not.
在此页面中,我添加了一个示例 jquery 代码,该代码应在下拉框更改时触发。我还添加了一些警报语句。但是,只显示第一个警报语句。一个里面的 jquery 代码没有。
I've tried to use jquery no conflict but it does not seem to work.
我试过使用 jquery 没有冲突,但它似乎不起作用。
Has someone faced this issue?
有人遇到过这个问题吗?
回答by Akrikos
I prefer using a self executing anonymous function to give your jQuery code its own scope where you can use $ as you normally would if you didn't have to worry about compatability.
我更喜欢使用自执行匿名函数为您的 jQuery 代码提供自己的范围,如果您不必担心兼容性,您可以像往常一样使用 $ 。
This is going to look weird if you haven't seen it before. Basically, what I did was define a function that takes a parameter (the $) and then execute that function with jQuery as the parameter.
如果你以前没有见过它,这看起来会很奇怪。基本上,我所做的是定义一个函数,该函数接受一个参数($),然后使用 jQuery 作为参数执行该函数。
<script>
jQuery.noConflict();
(function($) {
//use '$' as you normally would
$(document).ready(function() {
//code here that depends on the document being fully loaded
});
})(jQuery);
</script>
回答by Alec Smart
After you include jQuery, add the following in a script tag immediately after it.
包含 jQuery 后,立即在脚本标记中添加以下内容。
<script>
jQuery.noConflict();
jQuery(document).ready(function() {
alert('hi');
});
</script>
Also place your jQuery script before your mootools library. Order will be:
还将您的 jQuery 脚本放在您的 mootools 库之前。订单将是:
jQuery script include
jQuery 脚本包括
noConflict code
无冲突代码
mootools script include
mootools 脚本包括
回答by Santi
Insert the following before any jquery code:
在任何 jquery 代码之前插入以下内容:
$j = jQuery.noConflict(true);
Now you can use $j instead of $ for any jquery stuff you want to do. i.e.:
现在您可以使用 $j 而不是 $ 来处理您想做的任何 jquery 事情。IE:
$j("body")
回答by David Andres
Instead of j(function()...
而不是 j(function()...
try
尝试
j(document).ready
(
function()
{
alert("here");
j("select#rooms")
.change
(
function()
{
alert("here1");
}
)
}
);
You were trying to wrap a function instead of an element, which is what might be causing the unexpected behavior.
您试图包装一个函数而不是一个元素,这可能是导致意外行为的原因。