jQuery不使用变量。问题在于这一行:
http://www.whirloo.com/wp-content/plugins/contact-form-7/includes/css/styles.css?ver=4.9.2
http://www.whirloo.com/wp-content/plugins/woocommerce/assets/css/woocommerce-layout.css?ver=3.2.6
http://www.whirloo.com/wp-content/plugins/woocommerce/assets/css/woocommerce.css?ver=3.2.6
http://www.whirloo.com/wp-content/plugins/wordpress-social-login/assets/css/style.css?ver=4.9.1
http://www.whirloo.com/wp-content/plugins/js_composer/assets/css/js_composer.min.css?ver=5.4.5
https://fonts.googleapis.com/css?family=Open+Sans:400,600%7CRoboto:400,500,400italic
http://www.whirloo.com/wp-content/plugins/google-analytics-for-wordpress/assets/js/frontend.min.js?ver=6.2.6
http://www.whirloo.com/wp-includes/js/jquery/jquery.js?ver=1.12.4
http://www.whirloo.com/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.4.1
http://www.whirloo.com/wp-content/plugins/woocommerce/assets/js/frontend/add-to-cart.min.js?ver=3.2.6
http://www.whirloo.com/wp-content/plugins/js_composer/assets/js/vendors/woocommerce-add-to-cart.js?ver=5.4.5
http://www.whirloo.com/wp-content/themes/publisher/includes/libs/better-framework/assets/css/bs-icons.css
http://www.whirloo.com/wp-content/plugins/better-social-counter/css/style.min.css
http://www.whirloo.com/wp-content/themes/publisher/includes/libs/better-framework/assets/css/slick.min.css
http://www.whirloo.com/wp-content/plugins/better-adsmanager/css/bam.min.css
如果我将其更改为
$('img[alt = id]').trigger("click");
一切都按预期工作。我只是不能训练如何使用变量而不是引号内的值。我试过把id放在单/双引号中但没有运气。我确信这将是显而易见的事情。任何人都可以提供帮助。
这是完整的代码。
$('img[alt = "6"]').trigger("click");
答案 0 :(得分:3)
您可以组合一个字符串并使用它:
$('img[alt = "' + 6 + '"]').trigger("click");
这是一个变量:
var id = 6;
$('img[alt = "' + id + '"]').trigger("click");
它被称为string concatenation。
答案 1 :(得分:1)
观看此
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
示例:
$(`img[alt = "${id}"]`).trigger("click");