How to get all HTML elements with a certain class attribute?
I need to get all elements that use a certain class an开发者_开发百科d choose a random one.
How can I do this in jQuery?
var random_elem = $('.yourclass').eq(Math.floor(Math.random()*$('.yourclass').length));
Try this:
jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
random: function(a, i, m, r) {
if (i == 0) {
jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
};
return i == jQuery.jQueryRandom;
}
});
var randomElem = $(".className:random");
Orig article: http://blog.mastykarz.nl/jquery-random-filter/
精彩评论