代码之家  ›  专栏  ›  技术社区  ›  Jitendra Vyas

如何将此jquery代码转换为noconflict

  •  0
  • Jitendra Vyas  · 技术社区  · 15 年前

    我们需要用jquery替换每个$吗?

    $(document).ready(function() {
    
        $('.tabs a').click(function(){
            switch_tabs($(this));
        });
    
        switch_tabs($('.defaulttab'));
    
    });
    
    function switch_tabs(obj)
    {
        $('.tab-content').hide();
        $('.tabs a').removeClass("selected");
        var id = obj.attr("rel");
    
        $('#'+id).show();
        obj.addClass("selected");
    }
    
    3 回复  |  直到 15 年前
        1
  •  2
  •   Simon Quentin    15 年前

    只要您确定代码片段只包含jquery用法$

    (function($){
        $(document).ready(function() {
    
            $('.tabs a').click(function(){
                switch_tabs($(this));
            });
    
            switch_tabs($('.defaulttab'));
    
        });
    
        function switch_tabs(obj)
        {
            $('.tab-content').hide();
            $('.tabs a').removeClass("selected");
            var id = obj.attr("rel");
    
            $('#'+id).show();
            obj.addClass("selected");
        }
    })(jQuery);
    
        2
  •  1
  •   Pranay Rana    15 年前

    使用

    $.noConflict();
    
    
    (function($) { 
      $(function() {
        // more code using $ as alias to jQuery
      });
    })(jQuery);
    // other code using $ as an alias to the other library
    

    解决冲突问题

    更多细节: http://api.jquery.com/jQuery.noConflict/

        3
  •  0
  •   adardesign    15 年前
    var $j = jQuery.noConflict();
    
    
    $j(document).ready(function() {
    
    $j('.tabs a').click(function(){
        switch_tabs($j(this));
    });
    
    switch_tabs($j('.defaulttab'));
    
    });
    
    function switch_tabs(obj)
    {
    $j('.tab-content').hide();
    $j('.tabs a').removeClass("selected");
    var id = obj.attr("rel");
    
    $j('#'+id).show();
    obj.addClass("selected");
    }