javascript - x.addEventListener 不是函数吗?
问题描述
我正在做一个小项目来准备考试我有一些表格,我必须将每个表格的颜色随机更改为红色或绿色以及其他一些东西,但我的问题是颜色不会显示即使在我按下“开始”按钮之后。
你能看看我的代码并告诉我它有什么问题吗?如果有任何建议可以帮助我改进我的代码编写?
代码继续说 button.addeventlistener 不是一个函数。
我环顾了论坛,其中大多数人都在谈论更改脚本的位置,但这样做并没有帮助。我什至在索引文件中编写了整个脚本.. 这也没有改变任何东西谢谢
代码:
给定以下 HTML/CSS 代码,该代码绘制了一个包含 20 个按钮的表格,以及一个动作“开始”按钮:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style>
button:not(#start) {
height: 100px;
width: 100px;
}
#start {
font-size: 40px;
width: 310px;
text-align: center;
margin-left: 105px;
}
</style>
</head>
<body>
<table>
<tr>
<td><button></button></td>
<td><button></button></td>
<td><button></button></td>
<td><button></button></td>
<td><button></button></td>
</tr>
<tr>
<td><button></button></td>
<td><button></button></td>
<td><button></button></td>
<td><button></button></td>
<td><button></button></td>
</tr>
<tr>
<td><button></button></td>
<td><button></button></td>
<td><button></button></td>
<td><button></button></td>
<td><button></button></td>
</tr>
<tr>
<td><button></button></td>
<td><button></button></td>
<td><button></button></td>
<td><button></button></td>
<td><button></button></td>
</tr>
</table>
<button id="start">
START
</button>
<script>var rand;
// Math.floor((Math.random()*1)+1);
//question 1
var button = document.getElementsByTagName('button');
var td =document.getElementsByTagName('td');
var btn =document.getElementById('start');
btn.addEventListener('click',function(e){
console.log("ali");
for(var i=0 ;i< button.length;i++){
rand= Math.floor((Math.random()*2));
console.log(rand);
if(rand==1){
button[i].style.backgroundcolor="red";
console.log("red")
}
else if(rand==0){
button[i].style.backgroundcolor="green";
console.log("green")
}
}})
//question 2
button.addEventListener('click',function(e){
if(button.target.style.backgroundcolor=="red")
button.target.style.backgroundcolor=="";
})
//question 3
for (var i=0;i<button.length;i++){
if(button[i].style.backgroundcolor!="green")
i--;
if(i==button.length){
alert("You Won");
//question 4
for (var i=0;i<button.length;i++){
button[i].style.backgroundcolor=="";
}
}
}
//question 5
btn.addEventListener('click',function(e){
button[i].style.backgroundcolor=="";
})
</script>
</body>
</html>
解决方案
var button = document.getElementsByTagName('button');
结果是一个HTMLCollection(类似数组)
要将事件侦听器添加到目标,您必须遍历它们。
- 从 HTMLCollection 创建一个真正的数组
- 循环遍历它并向目标元素添加一个事件监听器。
var button = document.getElementsByTagName('button');
Array.from(button).forEach(e => {
e.addEventListener('click', function(e) {
console.log(this);
});
});
推荐阅读
- swift - 缺少标签栏。当我在 swift.for sidemenu 中打开 sidemenu 项目时,我正在使用 Drawercontroller
- javascript - 使用 websocket 执行 js 函数并等待返回值给 python 代码,反之亦然
- python - Pyspark 和 Python - 列不可迭代
- azure - 在 HTTP 连接器设置 LogicApps 中允许分块后仍然出现最大缓冲区大小错误
- html - 如何使我的标题背景颜色更小,同时保持响应
- python-3.x - 无法通过 Python 在 s3 存储桶上加载压缩的 csv 文件
- url - URL 格式:可以在 '?' 之前包含 '&' 在网址中?
- swift - 在 Swift 中裁剪肖像照片
- wordpress - 配置分页,以便从第二页开始使用不同的 Wordpress 模板
- sql-server - SQL Server:将实时数据从视图复制到表