我可以使用jQuery从元素中删除最后添加的类吗 [英] Can I remove just the last added class from an element with jQuery
问题描述
伙计们,这个问题几乎要问自己了……但是,为了更加清楚:
Hey guys, the question pretty much asks itself... however, for more clarity:
我有一个名为"chuckPalahniuk"的元素,它有名为"choke","fightclub"和"haunted"的类.
I have an element called "chuckPalahniuk" and it has classes named "choke", "fightclub" and "haunted".
当我单击"chuckPalahniuk"元素时,如何将其删除",先将其困扰",然后将其"fightclub"删除,将其单击,将其第三次删除,将其删除?
How could I get it so when I click on the "chuckPalahniuk" element, it removes "haunted" first, then "fightclub" on the second click and "choke" on the third?
也:请注意,类名是动态添加的.
also: be aware that the class names are dynamically added.
干杯. peeeps!
Cheers. peeeps!
psy.example:
psy.example:
$('#chuckPalahniuk').click(function() {
$(this).removeLastClassAdded(); //except this function doesn't exist...
});
推荐答案
只需将添加的每个类c.push('yourclass')
,然后是$(this).removeClass(c.pop());
just save in an array variable c
every class you add c.push('yourclass')
, then $(this).removeClass(c.pop());
http://www.devguru.com/technologies/ecmascript/quickref/pop.html
这篇关于我可以使用jQuery从元素中删除最后添加的类吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!