【问题标题】:jQuery: How to select class that includes two stringsjQuery:如何选择包含两个字符串的类
【发布时间】:2021-03-22 06:22:29
【问题描述】:

我有图像,每个图像都有两个类。这是 HTML 的相关部分。

class = "thing other0-a"
class = "thing other1-a"
class = "thing other2-a"
class = "thing other3-a"
class = "thing other0-b"
class = "thing other1-b"
class = "thing other2-b"

如何使用 jQuery 选择带有“thing”的类以及是否包含“-a”?

我知道如何做 [class*=-a],但我不知道如何结合选择完全匹配和包含匹配。

【问题讨论】:

    标签: javascript jquery class


    【解决方案1】:

    相当于jQuery中的AND

    $("[class*=thing][class*=-a]")
    

    相当于jQuery中的OR

    $("[class*=thing],[class*=-a]")
    

    NOT函数:not

    $(":not([class*=thing])")  means which doesnt contain "thing"
    

    【讨论】:

    【解决方案2】:

    您可以在选择器中使用所需的类声明两个属性。像这样:

    div[class*=-a][class*=thing]
    

    在这种情况下,匹配将是精确的。

    $('div[class*=-a][class*=thing]').css('color', 'green');
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class = "thing other0-a">text</div>
    <div class = "thing other1-a">text</div>
    <div class = "thing other2-a">text</div>
    <div class = "thing other3-a">text</div>
    <div class = "thing other0-b">text</div>
    <div class = "thing other1-b">text</div>
    <div class = "thing other2-b">text</div>

    【讨论】:

      猜你喜欢
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      • 2011-01-25
      • 1970-01-01
      • 2018-01-05
      • 2015-12-22
      • 2021-06-06
      • 2016-02-11
      相关资源
      最近更新 更多