jQuery check if element has a class beginning with some string

前端 未结 6 2050
面向向阳花
面向向阳花 2021-02-02 00:52

I need to loop through some elements in the page and then, for each one, if it had a class beginning with, for example, \"C\", do something.

$(\'#dialog li\').e         


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-02 01:08

    I don't think that there is a built-in selector to test for classes starting with a string.

    There is a selector to test if an attribute starts with a string, so if you know that your elements only have one class (or always start with the class in question), you could do:

    $(this).is("class^='C'")

    If, however, you can't guarantee either of the above conditions, you would have to manually split out and test each class defined on the element, as described here.

提交回复
热议问题