在 JavaScript/jQuery 中不使用 class 从游戏 space 中随机删除图像
Randomly remove image from game space without using class in JavaScript/jQuery
我试图让痣(https://jsfiddle.net/JennyF/s4rteuks/13/ 中的小立方体)在未被点击的情况下从 #gamespace
中随机消失。它们随机出现在 0 - 2 秒之间。该游戏在 Firefox 上运行,在包括我的尝试时没有解决这个问题,但它在 JSFiddle 中不起作用。
function addMole(){
xPos=randPosX();
yPos=randPosY();
$("#gamespace").append('<img src="img/mole.png" style="top:'+xPos+'px; left:'+yPos+'px;" />');
var r=Math.floor(Math.random()*(2000));
t=setTimeout("addMole();", r);
var count=0;
setTimeout("remove()", 1000);
function remove(){
$("#gamespace").append("<img id='img"+count+"'></img>");
count++;
setTimeout("remove()", 1000);
};
};//end mole
我的导师告诉我们不要使用 class
并给了我们这个例子。显然,我想 fadeOut
而不是添加任何内容。而且我没有任何 <p>
标签。我只是不知道用什么来代替他们。或者如果我完全搞砸了。
var count=0;
setTimeout("add()", 1000);
function add(){
$("div#div1").append("<p id='p"+count+"'></p>");
count++;
setTimeout("add()", 1000);}
好吧,这是更正后的代码和我的 jsfiddle https://jsfiddle.net/JennyF/s4rteuks/14/,它不起作用,但它在我的程序中起作用。我不知道我在 jsfiddle 中做错了什么。我准确地复制了所有内容并附上了正确的 jQuery 库。
function addMole(){
if (time_left >0){
xPos=randPosX();
yPos=randPosY();
$("#gamespace").append('<img id='+count+' src="img/mole.png" style="top:'+xPos+'px; left:'+yPos+'px;" />');
var f='#'+count;
count++;
$("#gamespace").find(f).delay(1500).fadeOut();
var r=Math.floor(Math.random()*(2000));
t=setTimeout("addMole();", r);
}else{
clearTimeout(t);
$("#gamespace").off("click", "img");
};//end if
};//end mole
我试图让痣(https://jsfiddle.net/JennyF/s4rteuks/13/ 中的小立方体)在未被点击的情况下从 #gamespace
中随机消失。它们随机出现在 0 - 2 秒之间。该游戏在 Firefox 上运行,在包括我的尝试时没有解决这个问题,但它在 JSFiddle 中不起作用。
function addMole(){
xPos=randPosX();
yPos=randPosY();
$("#gamespace").append('<img src="img/mole.png" style="top:'+xPos+'px; left:'+yPos+'px;" />');
var r=Math.floor(Math.random()*(2000));
t=setTimeout("addMole();", r);
var count=0;
setTimeout("remove()", 1000);
function remove(){
$("#gamespace").append("<img id='img"+count+"'></img>");
count++;
setTimeout("remove()", 1000);
};
};//end mole
我的导师告诉我们不要使用 class
并给了我们这个例子。显然,我想 fadeOut
而不是添加任何内容。而且我没有任何 <p>
标签。我只是不知道用什么来代替他们。或者如果我完全搞砸了。
var count=0;
setTimeout("add()", 1000);
function add(){
$("div#div1").append("<p id='p"+count+"'></p>");
count++;
setTimeout("add()", 1000);}
好吧,这是更正后的代码和我的 jsfiddle https://jsfiddle.net/JennyF/s4rteuks/14/,它不起作用,但它在我的程序中起作用。我不知道我在 jsfiddle 中做错了什么。我准确地复制了所有内容并附上了正确的 jQuery 库。
function addMole(){
if (time_left >0){
xPos=randPosX();
yPos=randPosY();
$("#gamespace").append('<img id='+count+' src="img/mole.png" style="top:'+xPos+'px; left:'+yPos+'px;" />');
var f='#'+count;
count++;
$("#gamespace").find(f).delay(1500).fadeOut();
var r=Math.floor(Math.random()*(2000));
t=setTimeout("addMole();", r);
}else{
clearTimeout(t);
$("#gamespace").off("click", "img");
};//end if
};//end mole