嘿所有,我不明白爲什麼這似乎沒有工作。 警報會彈出正確的ID,但removeClass似乎不會觸發。 我在做什麼錯?用變量作爲jQuery中的id來更改類?
function testClassSwitch(t_id) {
alert("Do things to a button with this ID: " + t_id);
$(t_id).removeClass("add-button");
}
謝謝!
嘿所有,我不明白爲什麼這似乎沒有工作。 警報會彈出正確的ID,但removeClass似乎不會觸發。 我在做什麼錯?用變量作爲jQuery中的id來更改類?
function testClassSwitch(t_id) {
alert("Do things to a button with this ID: " + t_id);
$(t_id).removeClass("add-button");
}
謝謝!
請問您的t_id
有#
id selector嗎?
哦,一個兒子! * facepalm *非常感謝,非常簡單。 ; -/ – TwstdElf 2010-08-13 17:13:59
不用擔心TwstdElf。這一個很容易錯過:P。 – 2010-08-13 17:25:19
@TwstdElf - 同意,容易犯的錯誤:) – rosscj2533 2010-08-13 17:55:35
在ID前加#
這樣
function testClassSwitch(t_id) {
alert("Do things to a button with this ID: " + t_id);
$('#' + t_id).removeClass("add-button");
}
適用於我:http://jsfiddle.net/sBJwJ/ – 2010-08-13 17:13:47
切換類使用toggleClass ...... – 2010-08-13 17:17:39