Javascript jQuery与Magento中的原型冲突
我在Magento中安装了此jQuery:Javascript jQuery与Magento中的原型冲突,javascript,jquery,magento,prototypejs,Javascript,Jquery,Magento,Prototypejs,我在Magento中安装了此jQuery: jQuery(document).ready(function($) { "use strict"; var firstItems = $('.item.first'); // Find tallest .item per row; match rest of row to it firstItems.each(function($) { var $first, row, headings, height
jQuery(document).ready(function($) {
"use strict";
var firstItems = $('.item.first');
// Find tallest .item per row; match rest of row to it
firstItems.each(function($) {
var $first, row, headings, heights, maxHeight;
$first = $(this);
row = $first.add($first.nextUntil(firstItems));
headings = row.find("h2, h5");
heights = headings.map(function($) {
return $(this).outerHeight();
});
maxHeight = Math.max.apply(null, heights);
headings.css("height", maxHeight);
});
});
可悲的是,它与原型相冲突。它抛出了一个错误:
[object object] is not a valid argument for 'Function.prototype.apply'
这让我相信冲突来自第15行:
maxHeight = Math.max.apply(null, heights);
有没有什么方法可以以不同的方式包装该函数,以便原型可以忽略它?您是一个jQuery对象,其中它应该是一个数组
heights = headings.map(function($) {
return $(this).outerHeight();
}).toArray(); //<-- convert to array
heights=headers.map(函数($){
返回$(this.outerHeight();
}).toArray()// 您可以尝试:
jQuery(function($){
code_with_$;
});
及
您还需要在原型上面声明jQuery。(您可以使用page.xml来定义jQuery在上述原型中的位置。)然后使用上述结构的jQuery代码,它将解决您的问题。这是一个非常好的答案!您详细解释了JS的工作原理,这有助于我(和其他观众)学习。我不接受像“只使用jQuery.noConflict();”@JezenThomas这样的快速修复答案,因为您使用的是本地范围的$
,而且您不会从jQuery冲突中看到这样的错误。看看你是否不想相信我的话;P@JezenThomas“快速修复答案”根本不是答案。您已经在使用一种好的技术(如@Esailija所提到的本地范围)--jQuery(document).ready(函数($){})
不需要使用jQuery.noConflict()
。您正在将$
作为参数传递给函数,这意味着您正在使用一个局部变量$
作为第一个参数,它始终是jQuery
。您可以使用function(){}
中的参数[0]
来检查这一点。无论如何,你的问题不同于noConflict
:)
(function($){
code_with_$;
})(jQuery);