dpd66100 2014-05-05 11:56
浏览 50

停止两个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>
  • 写回答

1条回答 默认 最新

  • dongzhu7329 2014-05-05 12:02
    关注

    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:

    Example: Load two versions of jQuery (not recommended). Then, restore jQuery's globally scoped variables to the first loaded 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();
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭