我的页面中有多个div:
<div imageId='image5' name='5' class='speechBubble'>
<div imageId='image5' name='5' class='speechBubble'>
<div imageId='image6' name='6' class='speechBubble'>
...
使用jquery,我想为给定的userId循环这些div。我需要说循环div具有类'speechBubble'和名称n(5,6)
谢谢
答案 0 :(得分:2)
$('.speechBubble').filter(function() {
return this.name.match(/^[56]{1}$/);
}).each(function(index, element) {
// TODO: do something with the element
// for example get the imageId attribute
var imageId = $(element).attr('imageId');
});
如果要匹配userId变量,则不需要过滤器功能。你可以这样做:
$('.speechBubble[name=' + userId + ']').each(function(index, element) {
// TODO: do something with the element
// for example get the imageId attribute
var imageId = $(element).attr('imageId');
});
答案 1 :(得分:1)
简单方法
$(".speechBubble").each(function(){
if($(this).attr("id")>=userId){ // assumin userId and id of .speechBubble as int vals
// take userId from where u have to loop
}
})
多数民众赞成你想要的东西?