2010-08-13 17 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