2

I have an element with multiple classes in it. When I click this class I need to get the remaining part of a class partial as a variable.

I know that the first part of the class will always start with "jump_"

example:

<span class="jump jump_toThis randomClass">Something</span>

$('#tray').on('click', 'span.jump', function(e){
    var $this     = $(this),
    classList = $this.attr('class');

    console.log(classList);
    // Not sure how to remove *jump_* from *toThis* 
    // along with the other classes

});
4

1 回答 1

1

尝试

$('#tray').on('click', 'span.jump', function (e) {
    var className = this.className,
        jumper = className.match(/\b(jump_.*?)\b/)[1];
    if (jumper) {
        $(this).removeClass(jumper).addClass(jumper.replace(/^jump_/, ''))
    }
});

演示:小提琴

如果您只想保留toThis课程,那么

$('#tray').on('click', 'span.jump', function (e) {
    var className = this.className,
        jumper = className.match(/\b(jump_.*?)\b/)[1];
    if (jumper) {
        $(this).removeClass().addClass(jumper.replace(/^jump_/, ''))
    }
});

演示:小提琴

于 2013-09-05T02:18:15.120 回答