JQuery单击功能不起作用

时间:2017-09-04 04:56:54

标签: javascript jquery html click jsfiddle

我试图让我的代码点击并显示提醒,但代码似乎无法正常工作。我在这里写了代码:

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

这是我的HTML:

<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 个答案:

答案 0 :(得分:2)

在jQuery选择器中使用多个类是使用多个句点完成的,例如

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

FIDDLE

classNames之间没有空格,它匹配具有所有类

的元素

答案 1 :(得分:2)

CSS选择器在这里是错误的。 正确的选择器:

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

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

有关CSS选择器的更多详细信息,请参阅此处:https://css-tricks.com/multiple-class-id-selectors/

答案 2 :(得分:2)

1忘了在每节课之前添加.

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

工作片段: -

&#13;
&#13;
$(".checkout-button.nsg-button.nsg-grad--heeb-orange").click(function() {
  alert('hohoho');
});
&#13;
<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>
&#13;
&#13;
&#13;

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

答案 3 :(得分:1)

请使用此

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

答案 4 :(得分:1)

这可能对您有用:

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

当我们有多个课程时,我们会添加。访问元素时的运算符