簡體   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