提问者:小点点

不能触发jQuery。对于包含特定文本的按钮,单击()?


我有一个按钮:

null

$('.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>

null

但是,当我点击它的时候,这是永远不会打印任何东西的?您可以在这张图片中看到,它正在找到这些元素,但在单击时没有触发。


共1个答案

匿名用户

您是在DOM加载之后加载脚本吗?尝试:

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