2010-08-13 49 views
1

嘿所有,我不明白爲什麼這似乎沒有工作。 警報會彈出正確的ID,但removeClass似乎不會觸發。 我在做什麼錯?用變量作爲jQuery中的id來更改類?

function testClassSwitch(t_id) { 
    alert("Do things to a button with this ID: " + t_id); 
    $(t_id).removeClass("add-button"); 
} 

謝謝!

+0

切換類使用toggleClass ...... – 2010-08-13 17:17:39

回答

6

請問您的t_id#id selector嗎?

+0

哦,一個兒子! * facepalm *非常感謝,非常簡單。 ; -/ – TwstdElf 2010-08-13 17:13:59

+0

不用擔心TwstdElf。這一個很容易錯過:P。 – 2010-08-13 17:25:19

+0

@TwstdElf - 同意,容易犯的錯誤:) – rosscj2533 2010-08-13 17:55:35

3

在ID前加#這樣

function testClassSwitch(t_id) { 
    alert("Do things to a button with this ID: " + t_id); 
    $('#' + t_id).removeClass("add-button"); 
} 
+0

適用於我:http://jsfiddle.net/sBJwJ/ – 2010-08-13 17:13:47