5个h3标签 使用js按照顺序改变颜色 颜色改为红色 改变后上一个标签恢复默认 一秒改变一次 重复执行 完整代码 vscode
<!DOCTYPE html>
<html>
<head>
<title>Change H3 Color</title>
<style>
h3 {
color: black;
}
</style>
</head>
<body>
<h3>Heading 1</h3>
<h3>Heading 2</h3>
<h3>Heading 3</h3>
<h3>Heading 4</h3>
<h3>Heading 5</h3>
<pre><code><script>
const headings = document.getElementsByTagName('h3');
let index = 0;
function changeColor() {
headings[index].style.color = 'red';
if (index > 0) {
headings[index-1].style.color = 'black';
}
index = (index + 1) % headings.length;
}
setInterval(changeColor, 1000);
</script>
</code></pre>
</body>
</html
原文地址: https://www.cveoy.top/t/topic/dq2i 著作权归作者所有。请勿转载和采集!