用jquery精简一下程序<!DOCTYPE html> <html lang="en"> <head> <title>Styles Example</title> <style type="text/css"> .myDiv { background-color: blue; width: 150px; height: 150px; margin-right: 20px; display: table-cell; vertical-align: middle; text-align: center } .highlight { font-size: xx-large; } span { display: block; color: yellow; text-align: center; } </style> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script> </head> <body> <table> <tr> <td> <div class="myDiv highlight"> <span>虎虎生威</span> </div> </td> <td> <div class="myDiv"> <span>生龙活虎</span> </div> </td> </tr> <tr> <td> <div class="myDiv"> <span>如虎添翼</span> </div> </td> <td> <div class="myDiv"> <span>卧虎藏龙</span> </div> </td> </table> <button onclick="start()" id="start">开始</button> <button onclick="stop()" disabled="disabled" id="stop">停止</button> <button onclick="resume()" disabled="disabled" id="resume">继续</button> <script> let divs = $(".myDiv"); //取得所有div let startBtn = $("#start"); //取得开始按钮 let stopBtn = $("#stop"); //取得停止按钮 let resumeBtn = $("#resume"); //取得继续按钮 let nums = [0, 1, 3, 2]; let index = 0, timerId; function start() { startBtn.prop("disabled", true); for (let num of nums) { divs.eq(nums[index]).removeClass("highlight"); } index = 0; divs.eq(nums[0]).addClass("highlight"); resume(); } function stop() { clearInterval(timerId); stopBtn.prop("disabled", true); startBtn.removeAttr("disabled"); resumeBtn.removeAttr("disabled"); } function resume() { resumeBtn.prop("disabled", true); stopBtn.removeAttr("disabled"); timerId = window.setInterval(function () { divs.eq(nums[index]).removeClass("highlight"); index = (index + 1) % 4; divs.eq(nums[index]).addClass("highlight"); }, 500); } </script> </body> </html>
时间: 2023-10-25 16:06:31 浏览: 110
<!DOCTYPE html>
<html lang="en">
<head>
<title>Styles Example</title>
<style type="text/css">
.myDiv {
background-color: blue;
width: 150px;
height: 150px;
margin-right: 20px;
display: table-cell;
vertical-align: middle;
text-align: center
}
.highlight {
font-size: xx-large;
}
span {
display: block;
color: yellow;
text-align: center;
}
</style>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<table>
<tr>
<td>
<div class="myDiv highlight">
<span>虎虎生威</span>
</div>
</td>
<td>
<div class="myDiv">
<span>生龙活虎</span>
</div>
</td>
</tr>
<tr>
<td>
<div class="myDiv">
<span>如虎添翼</span>
</div>
</td>
<td>
<div class="myDiv">
<span>卧虎藏龙</span>
</div>
</td>
</tr>
</table>
<button id="start">开始</button>
<button id="stop" disabled>停止</button>
<button id="resume" disabled>继续</button>
<script>
const divs = $(".myDiv");
const startBtn = $("#start");
const stopBtn = $("#stop");
const resumeBtn = $("#resume");
const nums = [0, 1, 3, 2];
let index = 0, timerId;
function start() {
startBtn.prop("disabled", true);
divs.eq(nums[index]).removeClass("highlight");
index = 0;
divs.eq(nums[0]).addClass("highlight");
resume();
}
function stop() {
clearInterval(timerId);
stopBtn.prop("disabled", true);
startBtn.removeAttr("disabled");
resumeBtn.removeAttr("disabled");
}
function resume() {
resumeBtn.prop("disabled", true);
stopBtn.removeAttr("disabled");
timerId = setInterval(function () {
divs.eq(nums[index]).removeClass("highlight");
index = (index + 1) % 4;
divs.eq(nums[index]).addClass("highlight");
}, 500);
}
startBtn.click(start);
stopBtn.click(stop);
resumeBtn.click(resume);
</script>
</body>
</html>
阅读全文