开发者

How to get all HTML elements with a certain class attribute?

开发者 https://www.devze.com 2023-03-22 06:20 出处:网络
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\

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

精彩评论

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