[英]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.