简体   繁体   English

如何将此jQuery代码转换为noconflict

[英]How to convert this jquery code into noconflict

will we have to replace every $ with jquery? 我们将不得不用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");
}

As long as you are sure the snippet contains just jQuery usage of $ then you can wrap it in a closure 只要您确定代码段仅包含$的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);

use 采用

$.noConflict();


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

Resolve the conflict issues 解决冲突问题

More detail : http://api.jquery.com/jQuery.noConflict/ 更多详细信息: http//api.jquery.com/jQuery.noConflict/

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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