我有兩個按鈕,其中一個類是「按鈕白色」,另一個是「按鈕白色小」類。在jQuery中,每當我點擊一個按鈕時,文本都會更改爲我的兩個按鈕:jQuery文本作用於重複類
$(document).ready(function() {
$(".button.white").click(function() {
$('.button.white').text('Added');
});
});
我該如何避免這種情況?
我有兩個按鈕,其中一個類是「按鈕白色」,另一個是「按鈕白色小」類。在jQuery中,每當我點擊一個按鈕時,文本都會更改爲我的兩個按鈕:jQuery文本作用於重複類
$(document).ready(function() {
$(".button.white").click(function() {
$('.button.white').text('Added');
});
});
我該如何避免這種情況?
$(".button.white").click(function() {
$(this).text('Added');
});
你可以使用this
獲取被點擊的按鈕:
$(document).ready(function() {
$(".button.white").click(function() {
$(this).text('Added');
});
});
一個起點,我會嘗試:
$(document).ready(function() {
$(".button.white").click(function() {
$('.button.white').not(".small).text('Added');
});
});
以去除。小項。
(不選,因爲我沒有獲得測試ATM)
這發生becouse它認爲每一個字都是一個不同勢類。 使用你的課程作爲按鈕白色和按鈕白色小'(沒有空白) 並在你的代碼中寫入:
$(".button-white").click(function() {
$(this).text('Added');
});