How to get random element in jquery?

后端 未结 7 1638
悲&欢浪女
悲&欢浪女 2020-12-03 01:04

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

So, if .class had 10 links, it would randomly

相关标签:
7条回答
  • 2020-12-03 01:37
    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.

    0 讨论(0)
提交回复
热议问题