JavaScript 改变 H3 标签颜色 - 每秒循环切换
<!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/kMAb 著作权归作者所有。请勿转载和采集!