jQuery的一个小例子
程序员文章站
2022-03-28 12:31:25
...
类似于粗糙版的打地鼠,地鼠会在九个单元格里随机出现,如果点中它,会提示“打中啦”,如果没点中,会提示“再接再厉”
html代码:
<body>
<table border="1" align="center" width="300" height="300">
<tr>
<td id="td0"></td>
<td id="td1"></td>
<td id="td2"></td>
</tr>
<tr>
<td id="td3"></td>
<td id="td4"></td>
<td id="td5"></td>
</tr>
<tr>
<td id="td6"></td>
<td id="td7"></td>
<td id="td8"></td>
</tr>
</table>
</body>
JS代码
<script src="../../20181203JQuery/js/jquery-3.1.1.min.js"></script>
<script>
var random = 0;
var time = 1000;
setInterval(function () {
var td1 = $("td");
random = Math.floor(Math.random()*9);
td1.css("backgroundImage","");
td1.eq(random).css({backgroundImage:"url('mouse2.gif')",backgroundRepeat:"no-repeat"});
td1.click(function(e){
var id = e.target.id;
var num = parseInt(id.substr(2));
if(num == random){
console.log("打中啦!");
}else{
console.log("再接再厉!");
}
});
},time)
</script>