本文介绍了jquery getElementsByClassName查找部分类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用javascript函数getElementsByClassName来循环一些没有确切类名的html div,但它以相同的文本开头:akordeon-item
I want to use the javascript function getElementsByClassName to loop in some html divs that don't have the exactly class name, but it starts with the same text: "akordeon-item"
因此,html文本如下所示:
So, the html text looks like this:
<div class="akordeon-item akordeon-item-first">
<div>
<span class="userid">
[email protected]
</span>
</div>
</div>
<div class="akordeon-item akordeon-item-second">
<div>
<span class="userid">
[email protected]
</span>
</div>
</div>
<div class="akordeon-item akordeon-item-third">
<div>
<span class="userid">
[email protected]
</span>
</div>
</div>
<div class="akordeon-item akordeon-item-fourth">
<div>
<span class="userid">
[email protected]
</span>
</div>
另外,我试过的javascript代码就是这个:
Also, the javascript code I tried is this one:
var slides = getElementsByClassName(".akordeon-item");
for(var i = 0; i < slides.length; i++)
{
alert("element : "+slides[i]);
}
我也尝试在getElementsByClassName方法中搜索此字符串:akordeon-item
I also tried searching for this string inside the getElementsByClassName method : "akordeon-item"
如何使这个循环工作?
请帮忙...
How can I make this loop work?Please help...
推荐答案
:
^= is starts with
$= is ends with
= is exactly equal
!= is not equal
*= is contains
尝试:
$.each($('[class^="akordeon-item"]'), function(key, value) {
console.log(key, value);
});
- jsFiddle demo
$.each
documentation
迭代集合的替代方法:
$('[class^="akordeon-item"]').each(function(key, value) {
console.log(key, value);
});
或使用代表
循环:
var slides = $('[class^="akordeon-item"]');
for (var i = 0; i < slides.length; i++) {
console.log(slides[i]);
}
这篇关于jquery getElementsByClassName查找部分类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!