开发者

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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜