trigger mouse enter on random element

北慕城南 提交于 2019-12-13 17:50:36

问题


I have this javascript:

$('.foto').filter(function(index) {
    return index == Math.floor(Math.random() * 8) + 1;
}).trigger('mouseover');

I want to simulate a hover effect on a photo, but somehow the filter function does not work. I also tried

$('.foto:random').trigger('mouseover');

回答1:


Try this:

$.fn.rand = function(){
    return this.eq(Math.floor(Math.random()*this.length));
};
$(".foto").rand().trigger("mouseover");

Note: you only have to define $.fn.rand once, usually right after including jquery.



来源:https://stackoverflow.com/questions/9525555/trigger-mouse-enter-on-random-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!