[英]Stop Conflict Between two jquery
I am working in one Web-base software, which is build in "codeigniter". 我正在使用一个基于Web的软件,该软件在“ codeigniter”中构建。 In this software they used jQueryUI, bootstrap JQuery etc.
在此软件中,他们使用了jQueryUI,引导JQuery等。
Here I want to implement inline editing functionality. 在这里,我想实现内联编辑功能。 I successfully implemented it but after Implementing inline editing functionality, other functionality of that not working properly because of conflict between two jQuery.
我成功实现了它,但是在实现了内联编辑功能之后,该功能的其他功能由于两个jQuery之间的冲突而无法正常工作。
Here is my jquery code which we added : 这是我们添加的我的jquery代码:
<script>
$(document).ready(function(){
$('div.auto').click(function(){
$('.ajax').html($('.ajax input').val());
$('.ajax').removeClass('ajax');
$(this).addClass('ajax');
$(this).html('<input id="editbox" size="'+$(this).text().length+'" type="text" value="' + $(this).text() + '">');
$('#editbox').focus();
}
);
</script>
I also put below code before above code for stopping jQuery conflict : 我还在下面的代码之前放置了以下代码,以停止jQuery冲突:
<script>
$.noConflict();
// Code that uses other library's $ can follow here.
</script>
https://api.jquery.com/jQuery.noConflict/ - $.noConflict()
releases control of $
, and assigns it the value previously assigned to it. https://api.jquery.com/jQuery.noConflict/-$
$.noConflict()
释放对$
控制,并为其分配先前分配给它的值。 Lower in the page you can see this: 在页面下方,您可以看到以下内容:
Example: Load two versions of jQuery (not recommended).
示例: 加载两个版本的jQuery(不推荐)。
Then, restore jQuery's globally scoped variables to the first loaded jQuery.
然后,将jQuery的全局范围内的变量还原到第一个加载的jQuery。
<div id="log">
<h3>Before $.noConflict(true)</h3>
</div>
<script src="//code.jquery.com/jquery-1.6.2.js"></script>
<script>
var $log = $( "#log" );
$log.append( "2nd loaded jQuery version ($): " + $.fn.jquery + "<br>" );
// Restore globally scoped jQuery variables to the first version loaded
// (the newer version)
jq162 = jQuery.noConflict( true );
$log.append( "<h3>After $.noConflict(true)</h3>" );
$log.append( "1st loaded jQuery version ($): " + $.fn.jquery + "<br>" );
$log.append( "2nd loaded jQuery version (jq162): " + jq162.fn.jquery + "<br>" );
Other than that, simply don't use $
in the global scope. 除此之外,根本不要在全局范围内使用
$
。 NEVER use $
in the global scope. 切勿在全局范围内使用
$
。 Use it only as a local parameter where it's needed. 仅在需要时将其用作局部参数。
jQuery(document).ready(function($) {
//you may now safely use $ inside here. Or if you pass a different parameter - use that
});
alternative: 替代方案:
jQuery(document).ready(function(jq) {
//you may now safely use jq inside here. Or if you pass a different parameter - use that
jq("#mydiv").addClass('ajax');
//now that $ is not jQuery, but another library, you can also use that library
$.nonJQueryFunctionOfSomeOtherLibraryUsing$Symbol();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.