如何在jquery中获取随机元素?

时间:2023-02-04 20:32:52

How can I return a random element in jQuery by doing something like $(.class).random.click()?

如何通过像$(。class).random.click()这样的东西在jQuery中返回一个随机元素?

So, if .class had 10 links, it would randomly click one of them.

因此,如果.class有10个链接,它会随机点击其中一个。

Here is what I did:

这是我做的:

var rand_num = Math.floor(Math.random()*$('.member_name_and_thumb_list a').size());
$(".member_name_and_thumb_list a").eq(rand_num).click();

6 个解决方案

#1


34  

var random = Math.floor(Math.random()*10);
$(".someClass").eq(random).click();

#2


53  

You can write a custom filter (taken from here):

您可以编写自定义过滤器(取自此处):

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;
    }
});

Example usage:

用法示例:

$('.class:random').click()

The same thing but as a plugin instead:

相同的事情,但作为插件而不是:

​jQuery.fn.random = function() {
    var randomIndex = Math.floor(Math.random() * this.length);  
    return jQuery(this[randomIndex]);
};

Example usage:

用法示例:

$('.class').random().click()

#3


37  

If you don't want to hard code the number of elements to choose from, this works:

如果您不想硬编码可供选择的元素数量,则可以:

things = $('.class');
$(things[Math.floor(Math.random()*things.length)]).click()

#4


9  

var rand = Math.floor(Math.random()*10);

$('.class').eq(rand).click();

Math.random() gets you a pseudo-random number between 0 and 1, so multiplying it by 10 and rounding it down gets you 0 to 9. .eq() is 0 indexed, so this will get you a random jQuery element out of the 10 you have.

Math.random()得到一个0到1之间的伪随机数,所以将它乘以10并向下舍入得到0到9. .eq()是0索引,所以这将得到一个随机的jQuery元素你有10个。

#5


2  

I'd suggest doing it the jQuery way using .eq() and .trigger().

我建议使用.eq()和.trigger()来实现jQuery方式。

$elements.eq(Math.floor(Math.random() * $elements.length)).trigger('click');

#6


1  

You can select random item by class name using jquery method eq()

您可以使用jquery方法eq()按类名选择随机项

see the example bellow.

看下面的例子。

var len = $(".class").length
var random = Math.floor( Math.random() * len ) + 1;
$(".class").eq(random).click();

#1


34  

var random = Math.floor(Math.random()*10);
$(".someClass").eq(random).click();

#2


53  

You can write a custom filter (taken from here):

您可以编写自定义过滤器(取自此处):

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;
    }
});

Example usage:

用法示例:

$('.class:random').click()

The same thing but as a plugin instead:

相同的事情,但作为插件而不是:

​jQuery.fn.random = function() {
    var randomIndex = Math.floor(Math.random() * this.length);  
    return jQuery(this[randomIndex]);
};

Example usage:

用法示例:

$('.class').random().click()

#3


37  

If you don't want to hard code the number of elements to choose from, this works:

如果您不想硬编码可供选择的元素数量,则可以:

things = $('.class');
$(things[Math.floor(Math.random()*things.length)]).click()

#4


9  

var rand = Math.floor(Math.random()*10);

$('.class').eq(rand).click();

Math.random() gets you a pseudo-random number between 0 and 1, so multiplying it by 10 and rounding it down gets you 0 to 9. .eq() is 0 indexed, so this will get you a random jQuery element out of the 10 you have.

Math.random()得到一个0到1之间的伪随机数,所以将它乘以10并向下舍入得到0到9. .eq()是0索引,所以这将得到一个随机的jQuery元素你有10个。

#5


2  

I'd suggest doing it the jQuery way using .eq() and .trigger().

我建议使用.eq()和.trigger()来实现jQuery方式。

$elements.eq(Math.floor(Math.random() * $elements.length)).trigger('click');

#6


1  

You can select random item by class name using jquery method eq()

您可以使用jquery方法eq()按类名选择随机项

see the example bellow.

看下面的例子。

var len = $(".class").length
var random = Math.floor( Math.random() * len ) + 1;
$(".class").eq(random).click();