代码之家  ›  专栏  ›  技术社区  ›  MGames

JQuery点击功能不工作

  •  0
  • MGames  · 技术社区  · 7 年前

    我试图让我的代码点击并显示警报,但代码似乎不起作用。我在这里编写了代码:

    https://jsfiddle.net/MGames/9eu94Lau/1/

    <div class="add-cart-button-col col-right">
    <a class="checkout-button nsg-button nsg-grad--heeb-orange" href="https://mycheckoutlinkgoeshere.com" data-query="https://linkcheckoutgoeshere.com">
                          CHECKOUT
                        </a>
                      </div>
    

    以下是在单击后不显示警报的代码

    $(".checkout-button nsg-button nsg-grad--heeb-orange").click(function() {
      alert('hohoho');
    });
    

    如果能帮我找出我做错了什么,我将不胜感激。非常感谢。

    5 回复  |  直到 7 年前
        1
  •  2
  •   Death-is-the-real-truth    7 年前

    . 每次上课前。

    2.删除所有类之间的空格(因为您正在尝试单个元素的所有类,而不是父子)

    $(".checkout-button.nsg-button.nsg-grad--heeb-orange").click(function() {
      alert('hohoho');
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="add-cart-button-col col-right">
      <a class="checkout-button nsg-button nsg-grad--heeb-orange" href="https://mycheckoutlinkgoeshere.com" data query="https://linkcheckoutgoeshere.com">CHECKOUT</a>
    </div>

    参考:- How can I select an element with multiple classes in jQuery?

        2
  •  2
  •   adeneo    7 年前

    在jQuery选择器中使用多个类是通过多个句点完成的,如下所示

    $(".checkout-button.nsg-button.nsg-grad--heeb-orange")
    

    FIDDLE

    类名之间没有空格,它匹配一个包含所有类的元素

        3
  •  2
  •   K K    7 年前

    CSS选择器在这里出错。

     $(".checkout-button.nsg-button.nsg-grad--heeb-orange").click(function() {
        alert('hohoho');
       });
    

    演示: https://jsfiddle.net/9eu94Lau/3/

    https://css-tricks.com/multiple-class-id-selectors/

        4
  •  1
  •   Yash Parekh niee    7 年前

    就用这个

    $("a[class='checkout-button nsg-button nsg-grad--heeb-orange']").click(function() {
      alert('hohoho');
    });
    
        5
  •  1
  •   Kunvar Singh    7 年前

    这可能对你有用:

    $(".checkout-button.nsg-button.nsg-grad--heeb-orange").click(function() {
      alert('hohoho');
    });
    

    . 操作人员 在访问元素时。