Javascript 如何在jquery中获取随机元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3614944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get random element in jquery?
提问by Josh
How can I return a random element in jQuery by doing something like $(.class).random.click()?
如何通过执行类似的操作在 jQuery 中返回一个随机元素$(.class).random.click()?
So, if .classhad 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();
采纳答案by Marko
var random = Math.floor(Math.random()*10);
$(".someClass").eq(random).click();
回答by Anurag
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()
回答by Moss
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()
回答by Yi Jiang
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,因此这将为您提供 10 个随机 jQuery 元素。
回答by lmeurs
I'd suggest doing it the jQuery way using .eq()and .trigger().
我建议使用.eq()和以 jQuery 方式执行此操作.trigger()。
$elements.eq(Math.floor(Math.random() * $elements.length)).trigger('click');
回答by Nishad Up
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();
回答by Aaron Plocharczyk
randojs.commakes this a simple one-liner:
randojs.com使这成为一个简单的单行:
rando($("a")).value[0].click()
The ".value" is there because you also have the option to get the index of the random jQuery element. The "[0]" is there to turn the jQuery element into a plain JavaScript element. If you add the following to the head of your html document, you can do pretty much whatever you want with randomness easily. Random values from arrays, random jquery elements, random properties from objects, and even preventing repetitions if needed.
“.value”在那里是因为您还可以选择获取随机 jQuery 元素的索引。“[0]”用于将 jQuery 元素转换为普通的 JavaScript 元素。如果您将以下内容添加到您的 html 文档的头部,您几乎可以轻松地随心所欲地做任何想做的事情。来自数组的随机值、随机的 jquery 元素、来自对象的随机属性,甚至在需要时防止重复。
<script src="https://randojs.com/1.0.0.js"></script>

