[英]JQuery My function only runs once
我试图使该功能选择一个随机的地方,并在提示时显示它。 当按下按钮时,它仅运行一次。
$(".place").hide();
var randomNumber = Math.floor(Math.random()*44);
$("#button").click(function() {
"use strict";
$("#place"+randomNumber).show("slow", function(){
alert=("New Place");
});
});
您需要为每次点击生成一个新的随机数
$("#button").click(function() {
"use strict";
var randomNumber = Math.floor(Math.random()*44);
$("#place"+randomNumber).show("slow", function(){
alert=("New Place");
});
});
如果要隐藏其他对象,请修改为:
$('.place').hide().filter("#place"+randomNumber).show...
您只需设置一次randomNumber
。 为了在用户单击#button
时生成新的随机数,可以使randomNumber
为函数:
$(".place").hide();
function randomNumber() {
return Math.floor(Math.random()*44);
}
$("#button").click(function(){
"use strict";
$("#place"+ randomNumber()).show(
"slow", function(){
alert=("New Place");
});
});
一旦定义,您的randomNumber将始终相同。 将其移动到点击处理程序中。
$(".place").hide();
$("#button").click(function() {
"use strict";
var randomNumber = Math.floor(Math.random()*44); // <---
$("#place"+randomNumber).show("slow", function(){
alert=("New Place");
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.