gpt4 book ai didi

jquery - 获取以某个字母开头的 jquery 类名

转载 作者:行者123 更新时间:2023-12-01 07:05:40 25 4
gpt4 key购买 nike

我有一个像这样的html,我需要获取以“border_”等字母开头的类名

<div class="box selected border_red"></div>

<div class="box selected border_blue"></div>

<div class="box border_pink inactive"></div>

<div class="box selected border_green"></div>

<div class="box border_grey inactive"></div>

jquery

$('.box').each(function(){

})

需要输出

border_red

border_blue

border_pink

border_green

border_grey

最佳答案

var check = "border_"; 
$('div.box[class*="border_"]').each(function () {
// Get array of class names
var cls = $(this).attr('class').split(' ');
for (var i = 0; i < cls.length; i++) {
// Iterate over the class and log it if it matches
if (cls[i].indexOf(check) > -1) {
console.log(cls[i].slice(0, cls[i].length));
}
}
});
.box{
width:100px;
height:100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box selected border_red"></div>

<div class="box selected border_blue"></div>

<div class="box border_pink inactive"></div>

<div class="box selected border_green"></div>

<div class="box border_grey inactive"></div>

受到这个问题的启发:JQuery get the rest of the element's class name that starts with string “whatever-”

关于jquery - 获取以某个字母开头的 jquery 类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46754653/

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