javascript - Can I remove just the last added class from an element with jQuery - Stack Overflow

Hey guys, the question pretty much asks itself... however, for more clarity:I have an element called &q

Hey guys, the question pretty much asks itself... however, for more clarity:

I have an element called "chuckPalahniuk" and it has classes named "choke", "fightclub" and "haunted".

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.

Cheers. peeeps!

psy.example:

$('#chuckPalahniuk').click(function() {
  $(this).removeLastClassAdded(); //except this function doesn't exist...
});

Hey guys, the question pretty much asks itself... however, for more clarity:

I have an element called "chuckPalahniuk" and it has classes named "choke", "fightclub" and "haunted".

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.

Cheers. peeeps!

psy.example:

$('#chuckPalahniuk').click(function() {
  $(this).removeLastClassAdded(); //except this function doesn't exist...
});
Share Improve this question edited Nov 11, 2010 at 15:05 Haim Evgi 126k46 gold badges222 silver badges226 bronze badges asked Nov 11, 2010 at 15:04 Barrie ReaderBarrie Reader 10.7k11 gold badges77 silver badges141 bronze badges 1
  • 1 get the class list by stackoverflow./questions/1227286/… and remove last entery – Haim Evgi Commented Nov 11, 2010 at 15:07
Add a ment  | 

3 Answers 3

Reset to default 4

just save in an array variable c every class you add c.push('yourclass'), then $(this).removeClass(c.pop());

http://www.devguru./technologies/ecmascript/quickref/pop.html

This will do it and will deal with leading and trailing whitespace, which a split()-based solution will not:

$('#chuckPalahniuk').click(function() {
    this.className = this.className.replace(/(^|\s+)[^\s]+(\s+)?$/, "");
});

Something like:

$('#chuckPalahniuk').click(function() {
    $(this).removeClass($(this).attr('class').split(/\s+/).pop());
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745147680a4613721.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信