gpt4 book ai didi

JQuery 类名选择器以?开头?

转载 作者:行者123 更新时间:2023-11-30 23:56:47 31 4
gpt4 key购买 nike

我有以下 html 标记:

<DIV class="bubble bubble_white">
<DIV class=bubble_large></DIV>
</DIV>
<DIV class="bubble bubble_black">
<DIV class=bubble_large></DIV>
</DIV>

我想选择类 bubble bubble_whitebubble bubble_black。我正在考虑下面的代码,但它不起作用:

$(".bubble.[class^=bubble_]")

关于如何做到这一点有什么想法吗?

最佳答案

[attr^=val] 选择器正在比较整个属性值。因此,您的属性值必须以 bubble_ 开头才能被选中。对于空格分隔的列表,您可以使用 [attr|=val] 选择器:

$(".bubble[class|=bubble_white], .bubble[class|=bubble_black]")

或者您自己进行过滤:

$(".bubble").filter("[class|=bubble_white], [class|=bubble_black]")

或者:

$(".bubble").filter(function() {
var $this = $(this);
return $this.hasClass("bubble_white") || $this.hasClass("bubble_black");
})

或者:

$(".bubble").filter(function() {
return /(?:^|\s+)bubble_/.test(this.className);
})

关于JQuery 类名选择器以?开头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1852106/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com