我想在表格单元格中添加一个eventListener,这样每次单击一个表格单元格来执行一个函数.
var getDaysInMonth = function (year, month) {
return new Date(year, month, 0).getDate();
}
var calendar = {
month: function () {
var d = new Date();
return d.getMonth() + this.nextMonth;
},
year: function () {
var y = new Date();
return y.getFullYear();
},
nextMonth: 1,
cellColor: 'white',
}
var loopTable = function () {
var daysInMonth = getDaysInMonth(calendar.year(), calendar.month());
var table = document.getElementById('myTable');
var rows = table.rows;
var l = 1;
var month = calendar.month();
var year = calendar.year(); …Run Code Online (Sandbox Code Playgroud)我的问题是:我正在尝试通过 Hot 类的 NODELIST 。我想将他们的 className 更改为 'cool' 。当我使用 for 循环时,它的第二个 li 似乎没有改变颜色。有谁知道我在这里犯了什么错误,第二个 li 元素没有改变颜色。
谢谢
var elements = document.getElementsByClassName('hot');
var i;
for(i = 0; i < elements.length; i++) {
elements[i].className = 'cool';
}Run Code Online (Sandbox Code Playgroud)
*{
box-sizing: border-box;
}
.hot {
background-color: red;
border: 1px solid black;
padding: 10px;
margin-top: 1px;
font-size: 25px;
list-style-type: none;
}
.cool {
background-color: blue;
padding: 10px;
color: white;
font-size: 25px;
}Run Code Online (Sandbox Code Playgroud)
<html>
<body>
<div id="page">
<h1 id="header">List</h1>
<h2>Buy Greoceries</h2>
<ul>
<li id="one" class="hot"><em>Fresh</em>figs</li>
<li id="two" …Run Code Online (Sandbox Code Playgroud)