所以我有这个css:
tag[class*="samepart-"] {
/* css code for all html classes with "samepart-" in their class name */
}
使用这个 html:
<div class="samepart-a">some html a</div>
<div class="samepart-b">some html b</div>
<div class="samepart-c">some html c</div>
现在我想在 JS 中获取所有带有“samepart-”的 html 类,而不需要向“samepart-”类添加额外的 html 类。
这是我尝试过的:
const htmlClasses1 = document.getElementsByClassName("samepart-");
console.log(htmlClasses1[0]);
const htmlClasses2 = document.getElementsByClassName('*["samepart-"]');
console.log(htmlClasses2[0]);
const htmlClasses3 = document.getElementsByClassName('[class*="samepart-"]');
console.log(htmlClasses3[0]);
const htmlClasses4 = document.getElementsByClassName(["samepart-"]);
console.log(htmlClasses4[0]);
div[class*="samepart-"] {
display: inline-block;
width: 120px;
height: 120px;
background-color: #00F;
color: #0FF;
}
<div class="samepart-a">some html a</div>
<div class="samepart-b">some html b</div>
<div class="samepart-c">some html c</div>
使用querySelectorAll()
:
PS:我编辑*=
为^=
因为您似乎只想要以samepart
.
const htmlClasses = document.querySelectorAll("div[class^='samepart-']");
console.log(htmlClasses);
div[class*="samepart-"] {
display: inline-block;
width: 120px;
height: 120px;
background-color: #00F;
color: #0FF;
}
<div class="samepart-a">some html a</div>
<div class="samepart-b">some html b</div>
<div class="samepart-c">some html c</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句