2013-02-23 52 views
0

我得到了2个名为“scripts1”的JS文件,其中包含幻灯片和“活动链接类”的脚本,另外一个包含“Mootools库”脚本的“script2”。
第一个问题是在“script1”中使用
“jQuery.noConflict()”解决的jquery和Mootools库之间的冲突。
但之后,“活动链接类”的脚本停止工作,如果我从“script1”中删除“jQuery.noConflict()”[但Mootools画廊将无法工作],这会很好地工作。
我只是猜测一定有一些关于$符号或某事的逻辑问题。如果是这样,PLZ解释背后的逻辑。
顺便说一句,我什至经历了以下解决方案,但没有结果!

  1. Jquery And Mootools, .noConflict is failing
  2. Jquery-Mootools conflict
  3. http://www.phil-taylor.com/2007/01/31/using-mootools-and-jquery-without-conflict/#.USjun6XI2ky

不管怎样,我把下面的代码,只是希望你能告诉我在哪里,我去错了。
文件“script1.js”包含以下代码:

jQuery(document).ready(function()  // the slideshow function 
{ 
    jQuery('#SlidesUl').fadeSlideShow(); 
}); 

$(function()       // the active link codes 
{ 
    var url = window.location.pathname, 
    urlRegExp = new RegExp(url.replace(/\/$/, '') + "$"); 
    $('#Menu div span #Menu1st a').each(function() 
    { 
     if (urlRegExp.test(this.href.replace(/\/$/, ''))) 
     { 
     $(this).addClass('active1st'); 
     } 
    }); 
}); 

jQuery.noConflict(); 

(function($)       // the slideshow options and all 
{ 
the codes 
})(jQuery);                  

和文件“script2.js”包含以下:

jQuery(document).ready(function ($) // the Mootools gallery codes 
{ 
    the codes 
});                

这里是伊夫如何把库和文件:

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.min.js" type="text/javascript"></script> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> 
<script src="js/script1.js" type="text/javascript"></script> 
<script src=" MooTools 1.4.4 " type="text/javascript"></script> 
<script src="js/script2.js" type="text/javascript"></script>      

任何帮助或解释,将不胜感激:)

回答

1

在调用jQuery.noConflict();(它在文档加载时执行)后,您的代码在the active link class中执行,因此您不能再使用$来引用jQuery,但是您会这样做。要将此更改$改为jQuery或将$设置为实际上是jQuery对象的ready函数的参数。

1.

$(function()       // the active link codes 
{ 
    var url = window.location.pathname, 
    urlRegExp = new RegExp(url.replace(/\/$/, '') + "$"); 
    jQuery('#Menu div span #Menu1st a').each(function() 
    { 
     if (urlRegExp.test(this.href.replace(/\/$/, ''))) 
     { 
     jQuery(this).addClass('active1st'); 
     } 
    }); 
}); 

2.

$(function ($)       // the active link codes 
{ 
    var url = window.location.pathname, 
    urlRegExp = new RegExp(url.replace(/\/$/, '') + "$"); 
    $('#Menu div span #Menu1st a').each(function() 
    { 
     if (urlRegExp.test(this.href.replace(/\/$/, ''))) 
     { 
     $(this).addClass('active1st'); 
     } 
    }); 
}); 
0

你的jQuery不打电话$.noConflict()是因为$.noConflict()将空出来的是默认使用jQuery中的$符号后,工作的原因。

我会建议将你的代码包装在一个匿名函数中。

// Null the $ symbol used by jQuery. 
$.noConflict(); 

// anonymous function. 
(function($) { 
    $(function() { 
     alert('DOM ready!'); 
    }); 
})(jQuery); 

http://jsfiddle.net/3v4CB/

编辑

试试这个:

// script1.js 
(function($) { 

    $(function() 
    { 
     $('#SlidesUl').fadeSlideShow(); // the slideshow function 

     // the active link codes 
     var url = window.location.pathname, 
     urlRegExp = new RegExp(url.replace(/\/$/, '') + "$"); 
     $('#Menu div span #Menu1st a').each(function() 
      { 
       if (urlRegExp.test(this.href.replace(/\/$/, ''))) 
      { 
       $(this).addClass('active1st'); 
      } 
     }); 

     // The codes 
    }); 

})(jQuery); 

// script2.js 
(function($) { 

    $(function() 
    { 
     // the Mootools gallery codes 
    }); 

})(jQuery);