javascript - 有人知道为什么第一个 onClick 事件不能正常工作,而第二个不能正常工作吗?
问题描述
我在尝试创建在第一个框中看到的 onClick 事件并通过创建在第二个框中看到的第二种方法来修复它时遇到了这个问题,但我不明白为什么第一种方法不起作用,有人知道为什么?
function test() {
document.body.style.padding = "0";
document.body.style.margin = "0";
document.body.style.backgroundColor = "#222";
let container = document.createElement("div");
let first_box = document.createElement("div");
let second_box = document.createElement("div");
container.appendChild(first_box);
container.appendChild(second_box);
document.body.appendChild(container);
container.style.height = "150px";
container.style.display = "flex";
container.style.justifyContent = "center";
container.style.alignItems = "center";
first_box.style.height = "50%";
first_box.style.width = "20%";
first_box.style.backgroundColor = "#fff";
first_box.style.cursor = "pointer";
first_box.style.marginRight = "2%";
second_box.style.height = "50%";
second_box.style.width = "20%";
second_box.style.backgroundColor = "#fff";
second_box.style.cursor = "pointer";
//onClick event
let btn1_status = 0;
let btn2_status = false;
if (btn1_status === 0) {
first_box.onclick = function() {
this.style.backgroundColor = "#456";
btn1_status = 1;
};
} else if (btn1_status === 1) {
first_box.onclick = function() {
this.style.backgroundColor = "#fff";
btn1_status = 0;
};
};
function box2() {
if (btn2_status === false) {
second_box.style.backgroundColor = "#654";
btn2_status = true;
} else if (btn2_status === true) {
second_box.style.backgroundColor = "#fff";
btn2_status = false;
};
};
second_box.onclick = function() {
box2();
};
};
console.log(test());
解决方案
问题在于first_box
:由于该test()
函数执行一次,它仅附加第一个事件侦听器,所有这部分:
} else if (btn1_status === 1) {
first_box.onclick = function() {
this.style.backgroundColor = "#fff";
btn1_status = 0;
};
};
实际上是死代码(假设函数运行一次)。多次运行test
实际上会添加一个事件监听器,造成执行的极大混乱(以及内存泄漏)。
这是一个更正的解决方案:
function test() {
document.body.style.padding = "0";
document.body.style.margin = "0";
document.body.style.backgroundColor = "#222";
let container = document.createElement("div");
let first_box = document.createElement("div");
let second_box = document.createElement("div");
container.appendChild(first_box);
container.appendChild(second_box);
document.body.appendChild(container);
container.style.height = "150px";
container.style.display = "flex";
container.style.justifyContent = "center";
container.style.alignItems = "center";
first_box.style.height = "50%";
first_box.style.width = "20%";
first_box.style.backgroundColor = "#fff";
first_box.style.cursor = "pointer";
first_box.style.marginRight = "2%";
second_box.style.height = "50%";
second_box.style.width = "20%";
second_box.style.backgroundColor = "#fff";
second_box.style.cursor = "pointer";
//onClick event
let btn1_status = 0;
let btn2_status = false;
first_box.onclick = function() {
if (btn1_status === 0) {
this.style.backgroundColor = "#456";
btn1_status = 1;
}else if (btn1_status === 1) {
this.style.backgroundColor = "#fff";
btn1_status = 0;
}
};
function box2() {
if (!btn2_status) {
second_box.style.backgroundColor = "#654";
btn2_status = true;
} else {
second_box.style.backgroundColor = "#fff";
btn2_status = false;
};
};
second_box.onclick = function() {
box2();
};
};
console.log(test());
推荐阅读
- python - 如何在不使用 itertools 导入组合的情况下从列表中获取所有项目对
- javascript - 如何使用 Angular Material 从图标创建下拉菜单?
- qt - QML Canvas:使用包含箭头的线条绘制 SVG 图像的问题
- java - Firebase 云消息传递 (FCM) 未获取数据
- node.js - 如何在同一调用中写入 Lambda 的临时目录?
- symfony - Symfony 4 - 如何在表单提交后为重定向中的路由设置实体 ID
- html - Bootstrap 4 Card Flex Width 在 Firefox 上无法正常工作
- javascript - 将 JS 值传递给 Php 函数而不重新加载页面
- angular - APP_INITIALIZER 阻止在错误时加载应用程序
- ssh - 即使使用 json 密钥文件进行身份验证,gcloud compute ssh 也需要密码