How to get random element in jquery?

后端 未结 7 1651
悲&欢浪女
悲&欢浪女 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:26

    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()
    

提交回复
热议问题