2017-09-04 84 views
-1

我試圖讓我的代碼點擊並顯示警告,但代碼似乎不起作用。我已經寫在這裏的代碼:JQuery單擊函數不工作

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'); 
}); 

後顯示警報代碼我希望能幫助我找出我做錯了什麼。謝謝。

編輯:這個問題是不同的,因爲我要求澄清我的代碼中存在的錯誤。這不是選擇與多類元素的副本...

+0

https://stackoverflow.com/questions/1041344/how-can-i-select-an-element-with-multiple-classes-in-jquery –

+0

'.checkout-button nsg-button nsg-grad-- heeb-orange'!=='.checkout-button .nsg-button .nsg-grad - heeb-orange' –

回答

2

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?

+0

兄弟我愛你! – MGames

+0

@MGames很高興幫助你的朋友:) :) –

2

在jQuery選擇使用多個類與多個時段進行,像這樣

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

FIDDLE

隨着之間沒有空格classNames,它匹配具有所有類的元素

1

只用這個

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

這可能會爲你工作:

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

當我們有多個類,那麼我們添加。運營商在訪問元素時。