【发布时间】:2022-01-07 14:13:33
【问题描述】:
我只能访问 h1 中的第一个元素。我还可以创建一个按钮并单击循环遍历 h1 元素并打开或关闭 .textDecoration 类。但我的目标是一步一步单击我想要的元素并打开或关闭 .textDecoration 类。
var x = document.getElementsByTagName("h1")[0];
function myFunction() {
x.classList.toggle("textDecoration");
}
x.addEventListener("click", myFunction);
.textDecoration {
text-decoration: line-through;
}
<h1>Text-Decoration: line-through</h1>
<h1>Text-Decoration: line-through</h1>
<h1>Text-Decoration: line-through</h1>
<h1>Text-Decoration: line-through</h1>
【问题讨论】:
标签: javascript html css toggle getelementsbytagname