jQuery与Magento中的原型冲突

jQuery conflicting with prototype in Magento

本文关键字:原型 冲突 Magento jQuery      更新时间:2023-09-26

我在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, 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);
    });
});

遗憾的是,它与Prototype相冲突。它抛出错误:

[object object] is not a valid argument for 'Function.prototype.apply'

这让我相信冲突来自第15行:

maxHeight = Math.max.apply(null, heights);

有没有什么方法可以用不同的方式包装这个函数,让原型忽略它?

您是.applying一个jQuery对象,其中它应该是一个数组。

    heights = headings.map(function($) {
        return $(this).outerHeight();
    }).toArray(); //<-- convert to array

原型库没有添加Function.prototype.apply,它是一个本地方法。

您可以尝试:

 jQuery(function($){
      code_with_$;
 });

(function($){
    code_with_$;
})(jQuery);

您还需要在原型之上声明jQuery。(您可以使用page.xml来定义jQuery在原型之上的位置。)然后使用上面提到的结构使用您的jQuery代码,它将解决您的问题。