繁体   English   中英

停止两个jQuery之间的冲突

[英]Stop Conflict Between two jquery

我正在使用一个基于Web的软件,该软件在“ codeigniter”中构建。 在此软件中,他们使用了jQueryUI,引导JQuery等。

在这里,我想实现内联编辑功能。 我成功实现了它,但是在实现了内联编辑功能之后,该功能的其他功能由于两个jQuery之间的冲突而无法正常工作。

这是我们添加的我的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>

我还在下面的代码之前放置了以下代码,以停止jQuery冲突:

<script>
$.noConflict();
// Code that uses other library's $ can follow here.
</script>

https://api.jquery.com/jQuery.noConflict/-$ $.noConflict()释放对$控制,并为其分配先前分配给它的值。 在页面下方,您可以看到以下内容:

示例:

<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>" );

除此之外,根本不要在全局范围内使用$ 切勿在全局范围内使用$ 仅在需要时将其用作局部参数。

jQuery(document).ready(function($) {
    //you may now safely use $ inside here. Or if you pass a different parameter - use that
});

替代方案:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM