gpt4 book ai didi

javascript - 在 js 中访问 html 类,如 css `tag[class*="samepart -"]`

转载 作者:行者123 更新时间:2023-11-30 09:24:17 25 4
gpt4 key购买 nike

<分区>

所以我有这个 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>

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