在同一页面中同时使用Prototype和jQuery的问题

当我使用jquery脚本进行内联窗口和原型来设置菜单时,我遇到了冲突。 当我使用其中一个工作正常。 但是,当我同时使用它们时,只有原型才有效。 我读了一个关于jquery.noconflict的内容,但我可以正确使用它。 那些是脚本。

这是我的jquery脚本(内联窗口)

 $(document).ready(function(){ //When you click on a link with class of poplight and the href starts with a # $('a.poplight[href^=#]').click(function() { var popID = $(this).attr('rel'); //Get Popup Name var popURL = $(this).attr('href'); //Get Popup href to define size //Pull Query & Variables from href URL var query= popURL.split('?'); var dim= query[1].split('&'); var popWidth = dim[0].split('=')[1]; //Gets the first query string value //Fade in the Popup and add close button $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend(''); //Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css var popMargTop = ($('#' + popID).height() + 80) / 2; var popMargLeft = ($('#' + popID).width() + 80) / 2; //Apply Margin to Popup $('#' + popID).css({ 'margin-top' : -popMargTop, 'margin-left' : -popMargLeft }); //Fade in Background $('body').append('
'); //Add the fade layer to bottom of the body tag. $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer return false; }); //Close Popups and Fade Layer $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer... $('#fade , .popup_block').fadeOut(function() { $('#fade, a.close').remove(); }); //fade them both out return false; }); });

这是我的原型脚本

   //   

在嵌入的jquery.js之后放右:

  

并改变这一行:

 $(document).ready(function(){ 

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

首先加载jQuery然后调用

 jQuery.noConflict(); //if loaded first, you could also use $.noConflict(), //but consistency is a good thing 

然后继续加载剩下的东西(包括Prototype,自定义脚本等)。

最后(这与你上面的第一个例子有关)使用函数jQuery,你通常会使用$。


你的基本问题是Prototype和jQuery都使用$作为另一个函数的引用。 不幸的是,Prototype或多或少需要它的$定义,这意味着一旦加载,没有别的东西应该命名为$。 jQuery的noConflict方法摆脱了$方法来防止这个问题。

1.-加载jQuery库。 2.-你的jQuery东西3.-加载原型库4.-做你的Prototype东西

你应该在jQuery上使用noConflict函数,你的jQuery东西应该以这种方式启动:

  

如果您需要更多信息,请查看jQuery API http://api.jquery.com/jQuery.noConflict/