Loop through divs with given class and name
I have multiple divs in my page as:
<div imageId='image5' name='5' class='speechBubble'>
<开发者_JAVA技巧;div imageId='image5' name='5' class='speechBubble'>
<div imageId='image6' name='6' class='speechBubble'>
...
With jquery, I want to loop these divs for the given userId. I need to say loop divs that have class 'speechBubble' and name n (5,6)
Thank you
You could use the .filter()
and .each()
functions:
$('.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');
});
If you want to match against a userId variable you don't need the filter function. You could do this:
$('.speechBubble[name=' + userId + ']').each(function(index, element) {
// TODO: do something with the element
// for example get the imageId attribute
var imageId = $(element).attr('imageId');
});
Easy Way
$(".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
}
})
Thats what you wanted ?
精彩评论