1

我有这个随机显示/隐藏 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 称为错误?

4

1 回答 1

1

您也可以使用jQuery 代替$. 并离开$其他图书馆。这样你就不会面临冲突问题。

像这样试试。

jQuery(document).ready(function() {
      jQuery('.control').on('click', function(e) {
          e.preventDefault();
于 2012-07-27T17:05:04.977 回答