两个脚本之间可能存在冲突

时间:2012-07-27 17:03:32

标签: javascript jquery

我有这个脚本随机显示/隐藏div。但由于某些原因,它在页面加载时没有显示任何内容:

$(document).ready(function() {
      $('.control').on('click', function(e) {
          e.preventDefault();
          var field = $(this).data('field');
          $('.hider:visible').fadeOut("slow", function() {
              $('#' + field + '-gallery, #' + field + '-tag').fadeIn("slow");
          });
      });

      var randomIndex = Math.floor((Math.random() * 100) + 1) % 5;
      console.log(randomIndex);

      var field = $($('a').get(randomIndex)).data('field');
      $('#' + field + '-gallery, #' + field + '-tag').fadeIn("slow");
  });

我有这个(在页脚中):

/* jQuery.noConflict() for using the plugin along with other libraries. 
           You can remove it if you won't use other libraries (e.g. prototype, scriptaculous etc.) or 
           if you include jQuery before other libraries in yourdocument's head tag. 
           [more info: http://docs.jquery.com/Using_jQuery_with_Other_Libraries] */
        //jQuery.noConflict(); 
        /* calling thumbnailScroller function with options as parameters */
        (function($){
        window.onload=function(){ 
            $("#tS2").thumbnailScroller({ 
                    scrollerType:"clickButtons", 
                    scrollerOrientation:"horizontal", 
                    scrollSpeed:2, 
                    scrollEasing:"easeOutCirc", 
                    scrollEasingAmount:600, 
                    acceleration:10, 
                    scrollSpeed:900, 
                    noScrollCenterSpace:10, 
                    autoScrolling:0, 
                    autoScrollingSpeed:2000, 
                    autoScrollingEasing:"easeInOutQuad", 
                    autoScrollingDelay:500 

            });
        }
        })(jQuery);

我注意到如果我在页脚的脚本中启用jQuery.noConflict,我会在顶层脚本的检查器中收到错误:

$(document).ready(function() {
      $('.control').on('click', function(e) {
Uncaught TypeError: Property '$' of object [object Window] is not a function
          e.preventDefault();

但是,如果我在页脚脚本中禁用noConflict,则没有错误,但顶级脚本仍无法正常工作。我是否需要在顶级脚本中添加另一个noConflict,或者我使用$(document).ready来调用它是错误的。

1 个答案:

答案 0 :(得分:1)

您也可以使用jQuery代替$。将$留给其他图书馆。这样你就不会遇到冲突问题。

试试这样。

jQuery(document).ready(function() {
      jQuery('.control').on('click', function(e) {
          e.preventDefault();