2012-11-16 108 views
0

那么我读了一些线程和jQuery的维基,但不知何故我无法解决我的问题。jQuery冲突 - 使用jQuery-noconflict

试图添加jQuery.noConflict(),因为如果我运行一些jQuery库冲突。如果我添加jQuery.noConflict(),就像它已经在jQuery Wiki上告知的那样。这两个脚本都不起作用。

脚本

$(document).ready(function() { 

    //Default Action 
    $(".tab_content").hide(); 
    $("ul.tabs li:first").addClass("active").show(); 
    $(".tab_content:first").show(); 

    //On Click Event 
    $("ul.tabs li").click(function() { 
     $("ul.tabs li").removeClass("active"); 
     $(this).addClass("active"); 
     $(".tab_content").hide(); 
     var activeTab = $(this).find("a").attr("href"); 
     $(activeTab).fadeIn(); 
     return false; 
    }); 

}); 

如果我在这里新增jQuery.noConflict()

$.noConflict(); 
    jQuery(document).ready(function($) { 

    //Default Action 
    $(".tab_content").hide(); 
    $("ul.tabs li:first").addClass("active").show(); 
    $(".tab_content:first").show(); 

    //On Click Event 
    $("ul.tabs li").click(function() { 
     $("ul.tabs li").removeClass("active"); 
     $(this).addClass("active"); 
     $(".tab_content").hide(); 
     var activeTab = $(this).find("a").attr("href"); 
     $(activeTab).fadeIn(); 
     return false; 
    }); 

}); 

回答

0

尝试使用这个 -

jQuery.noConflict()(function(){ 
    // code using jQuery 
}); 

如果这不起作用尝试使用HTML,CSS和JS小提琴,给有问题的链接。

+0

现在感谢它的工作.. – karabey

0

我认为你应该使用它想:

jQuery(document).ready(function($) { 
    $.noConflict(); 

    // then the code stuff 
    }); 

看的作品。

0

您可以使用它像这样(从jQuery docs):

<script type="text/javascript" src="other_lib.js"></script> 
<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript"> 
    $.noConflict(); 
    jQuery(document).ready(function($) { 
    // Code that uses jQuery's $ can follow here. 
    }); 

    // Code that uses other library's $ can follow here. 
</script> 
+0

两个或多个jQuery代码之间的冲突呢?我只使用jQuery代码,没有其他库。我使用的是相同的代码,但这并不能解决问题。 – karabey

+0

@karabey:对不起,我错过了那部分。只是好奇,为什么你使用多个jQuery实例? –

0

我会建议更改代码弄成这个样子

(function($){ 
    // code using jQuery with $. 
}(jQuery)); 

这将被要求的文件准备和你将不再需要$.noConflict();