[英]How to resolve jQuery conflict?
I have a jQuery custom file which is disabling or conflicting my other jQuery file, please how do i resolve this. 我有一个jQuery自定义文件,它正在禁用或与其他jQuery文件冲突,请如何解决此问题。 Please see below: My Custom file:
请参见以下内容:我的自定义文件:
$(".search").keyup(function()
{
var searchid = $(this).val();
var dataString = 'search='+ searchid;
if(searchid!='')
{
$.ajax({
type: "POST",
url: "search.php",
data: dataString,
cache: false,
success: function(html)
{
$("#result").html(html).show();
}
});
}return false;
});
Conflicting Files: 冲突的文件:
<script src="js/jquery.isotope.min.js"></script>
<script src="js/nprogress.js"></script>
I would be more than happy if this is resolved. 如果这个问题解决了,我会很高兴。
I'm assuming that you know for certain jQuery is conflicting - you've checked the console errors, etc. 我假设您知道某些jQuery存在冲突-您已经检查了控制台错误等。
You could wrap your jQuery in a self-executing anonymous function as follows: 您可以将jQuery包装在一个自动执行的匿名函数中,如下所示:
(function($) {
$(".search").keyup(function()
{
var searchid = $(this).val();
var dataString = 'search='+ searchid;
if(searchid!='')
{
$.ajax({
type: "POST",
url: "search.php",
data: dataString,
cache: false,
success: function(html)
{
$("#result").html(html).show();
}
});
}return false;
});
})(jQuery);
EDIT - a bit of explanation of what's happened above. 编辑-上面发生的事情的一些解释。 In order to prevent any potential conflicts with other scripts / frameworks we are passing the jQuery object as an argument to our function (hence
function($)
). 为了防止与其他脚本/框架的任何潜在冲突,我们将jQuery对象作为参数传递给我们的函数(因此,
function($)
)。 The benefits of doing this is that you can now use $
locally within the function at will. 这样做的好处是您现在可以随意在函数中本地使用
$
。 Without fear of conflicting with other scripts on a global
scope. 不必担心与
global
范围内的其他脚本发生冲突。
I Was able to fix the problem by just wrapping it with: 我仅用以下命令就可以解决该问题:
$(document).ready(function() {
//-----
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.