jquery find element by specific class when element has multiple classes

后端 未结 5 1176
Happy的楠姐
Happy的楠姐 2021-01-30 22:15

So I am working on something that wasn\'t well thought out in the build from the backend team. That leaves me with a document full of divs.

What I am doing is rolling b

相关标签:
5条回答
  • 2021-01-30 22:27
    var divs = $("div[class*='alert-box']");
    
    0 讨论(0)
  • 2021-01-30 22:28

    You can combine selectors like this

    $(".alert-box.warn, .alert-box.dead");
    

    Or if you want a wildcard use the attribute-contains selector

    $("[class*='alert-box']");
    

    Note: Preferably you would know the element type or tag when using the selectors above. Knowing the tag can make the selector more efficient.

    $("div.alert-box.warn, div.alert-box.dead");
    $("div[class*='alert-box']");
    
    0 讨论(0)
  • 2021-01-30 22:30

    You can select elements with multiple classes like so:

    $("element.firstClass.anotherClass");
    

    Simply chain the next class onto the first one, without a space (spaces mean "children of").

    0 讨论(0)
  • 2021-01-30 22:43

    you are looking for http://api.jquery.com/hasClass/

    <div id="mydiv" class="foo bar"></div>
    
    $('#mydiv').hasClass('foo') //returns ture
    
    0 讨论(0)
  • 2021-01-30 22:46

    An element can have any number of classNames, however, it can only have one class attribute; only the first one will be read by jQuery.

    Using the code you posted, $(".alert-box.warn") will work but $(".alert-box.dead") will not.

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