我的问题是在类名中找到具有特定前缀的所有对象,就像在html的以下部分中一样,我希望能够用单个语句获得us_mr和us_she。
<div>
<div class="us_me">Some about me</div>
<div class="us_she">Some about she</div>
<div class="they_she">Some about others</div>
</div>
this what I try
$(".^us_")
假设它会找到
<div class="us_me">Some about me</div>
<div class="us_she">Some about she</div>
但是出现语法错误
您可以尝试使用$("div[class^='us_']")
工作演示
console.log($("div[class^='us_']").length);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<div class="us_me">Some about me</div>
<div class="us_she">Some about she</div>
<div class="they_she">Some about others</div>
</div>
通配符的正确语法是:[attribute^="value"]
.
的例子:
$("[class^='us_']").addClass('someClass');
.someClass {
background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<div class="us_me">Some about me</div>
<div class="us_she">Some about she</div>
<div class="they_she">Some about others</div>
</div>