我正在使用Amazon文档在WordPress网站上集成Amazon Webstore(Amazon Checkout)here. 我已经有一段时间没有使用外部JScript排队了。我需要从Amazon加载两个脚本。我为函数添加了一些代码。php,但我似乎犯了一个错误,并且不断出现以下错误:
TypeError: \'undefined\' is not a function (evaluating \'$(jQuery(document).find("#globalParameters"))\').
在merchant\\u购物车上。js:5
代码如下:
function img_scripts_with_jquery()
{
// Register the script like this for a theme:
wp_register_script( \'aws-cba\', \'https://images-na.ssl-images-amazon.com/images/G/01/cba/js/common/cba_shared.js\', array( \'jquery\' ));
// For either a plugin or a theme, you can then enqueue the script:
wp_enqueue_script( \'aws-cba\' );
//second script
wp_register_script( \'aws-merchant-cart\', \'https://images-na.ssl-images-amazon.com/images/G/01/cba/js/shoppingcart/merchant_cart.js\', array( \'jquery\' ));
// For either a plugin or a theme, you can then enqueue the script:
wp_enqueue_script( \'aws-merchant-cart\' );
}
add_action( \'wp_enqueue_scripts\', \'img_scripts_with_jquery\' );
我在这里犯了什么编码错误?这两个需要jQuery的外部脚本的排队是否正常?
Update
在页脚中加载脚本会让事情变得更好,但当我添加按钮来加载购物车时,仍然出现了相同的错误。当前脚本现在为:
/Enqueue AWS Scripts
function img_scripts_with_jquery()
{
// Register the First Script:
wp_register_script( \'aws-cba\', \'https://images-na.ssl-images-amazon.com/images/G/01/cba/js/common/cba_shared.js\', array( \'jquery\' ),\'version\', true );
// Enqueue it:
wp_enqueue_script( \'aws-cba\' );
//Register Second Script
wp_register_script( \'aws-merchant-cart\', \'https://images-na.ssl-images-amazon.com/images/G/01/cba/js/shoppingcart/merchant_cart.js\', array( \'jquery\' ), \'version\', true);
// Enqueue this one too:
wp_enqueue_script( \'aws-merchant-cart\' );
}
add_action( \'wp_enqueue_scripts\', \'img_scripts_with_jquery\' );
Update II
Firebug将错误描述如下:
TypeError: $ is not a function
https://images-na.ssl-images-amazon.com/images/G/01/cba/js/shoppingcart/merchant_cart.js?ver=version
Line 5
似乎是jQuery冲突。。
最合适的回答,由SO网友:Max Yudin 整理而成
尝试保存jQuery.noConflict();
像jquery-no-conflict.js
. 然后使用适当的依赖项将所有内容排队:
<?php
function img_scripts_with_jquery() {
wp_enqueue_script(
\'jquery-no-conflict\',
\'path/to/jquery-no-conflict.js\',
array(
\'jquery\'
),
\'version\',
true
);
wp_enqueue_script(
\'aws-cba\',
\'https://images-na.ssl-images-amazon.com/images/G/01/cba/js/common/cba_shared.js\',
array(
\'jquery-no-conflict\'
),
\'version\',
true
);
wp_enqueue_script(
\'aws-merchant-cart\',
\'https://images-na.ssl-images-amazon.com/images/G/01/cba/js/shoppingcart/merchant_cart.js\',
array(
\'jquery-no-conflict\'
),
\'version\',
true
);
}
add_action( \'wp_enqueue_scripts\', \'img_scripts_with_jquery\' );