jQuery: selector (classname with space)

后端 未结 4 991
清歌不尽
清歌不尽 2020-11-29 21:23

I\'m trying to get a div which has \"panel current\" as classname. The problem is the space - how can I select it?

相关标签:
4条回答
  • 2020-11-29 21:29

    Class names can't have spaces in them. What you have there is two classes:

    <div class="panel current">
    

    This div has two classes: panel and current. That's easily selected:

    $("div.panel.current")...
    

    That means select all divs that have class panel and class current.

    0 讨论(0)
  • 2020-11-29 21:38
    $('div').filter(function() {
        return this.className == 'panel current';
    });
    

    OR

    $("div[class='panel current']");
    

    Use this if you need to match an element with an exact match of class names (including spaces)

    The other posters are right, the DiV you posted has two class names: 'panel' and 'current'; If you want to select them both, use $('.panel.current').

    This will also include elements like:

    <div class="foo panel bar current"></div>
    
    0 讨论(0)
  • 2020-11-29 21:50

    panel current is not a class name, actually it is two class names. You could use the following selector:

    $('.panel.current')
    
    0 讨论(0)
  • 2020-11-29 21:50

    The div has two class names:

    • panel
    • current

    You can use $("div.panel") or $("div.current") to select it.

    0 讨论(0)
提交回复
热议问题