我有这个javascript:

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

我想在照片上模拟悬停效果,但不知何故,过滤功能不起作用。 我也试过

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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top