简体   繁体   中英

Stop Conflict Between two jquery

I am working in one Web-base software, which is build in "codeigniter". In this software they used jQueryUI, bootstrap JQuery etc.

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.

Here is my jquery code which we added :

<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 :

<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. Lower in the page you can see this:

<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();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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