我试图让我的代码单击并显示警报,但代码似乎不起作用。我在这里编写了代码:
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');
});
如果您能帮助我找出我做错了什么,我将不胜感激。谢谢。
编辑:这个问题是不同的,因为我要求澄清我的代码中存在的错误。它不是选择具有多个类的元素的重复...
最佳答案
1 忘记在每个类之前添加.
。
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?
关于javascript - JQuery 点击功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46030473/