简体   繁体   English

无法为包含特定文本的按钮触发 jQuery.click()?

[英]Cannot trigger jQuery .click() for button that contains certain text?

I have a button:我有一个按钮:

 $('.add_to_cart span:contains("Choose a Size")').click(function() { console.log("it has been clicked") });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button type="submit" name="add" id="add-to-cart" class="action_button add_to_cart v-center" data-variantcount="44" data-label="Add to Cart" disabled=""> <span class="text">Choose a Size</span> </button>

However, this is never printing anything when I click it?但是,当我单击它时,它永远不会打印任何东西? You can see in this image that it is finding these elements but not triggering when clicked.您可以在此图像中看到它正在查找这些元素,但在单击时不会触发。

Are you loading the script after the DOM has loaded?您是在 DOM 加载后加载脚本吗? Try:尝试:

$(function() {
  $('.add_to_cart span:contains("Choose a Size")').click(function () {
    console.log("it has been clicked")
   });
});

Could you try this?你能试试这个吗?

$(document).on('click','.add_to_cart span:contains("Choose a Size")', function(){
console.log("it has been clicked");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM