英文:
How to make newly cloned buttons work with an existing eventlistener?
问题
在按下"leave"按钮时,我希望屏幕上显示10个新的"leave"按钮。当点击这些按钮中的任何一个时,它们每个都需要在屏幕上显示10个新的按钮。
但是只有最初的"leave"按钮会显示10个新的按钮,新克隆的按钮不会显示。
JavaScript 代码:
const container = document.querySelector('.container');
const leaveButton = document.querySelector('#leave');
leaveButton.addEventListener('click', () => {
for (let i = 0; i < 10; i++) {
const newButton = document.createElement('button');
newButton.innerHTML = leaveButton.innerHTML;
newButton.style.top = Math.random() * window.innerHeight + 'px';
newButton.style.left = Math.random() * window.innerWidth + 'px';
newButton.setAttribute("id", "leave");
container.appendChild(newButton);
}
});
HTML 代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>PressTheButton</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<button id="leave">离开</button>
<h1>这很简单</h1>
<p>只需点击按钮</p>
</div>
<script src="script.js"></script>
</body>
</html>
英文:
On the press of the button 'leave' I want 10 new 'leave' buttons to be displayed on the screen. Each one of these buttons needs to display 10 new buttons on the screen when clicked.
But only the original 'leave' button displays 10 new buttons, the newly cloned ones don't.
Javascript code:
const container = document.querySelector('.container');
const leaveButton = document.querySelector('#leave');
leaveButton.addEventListener('click', () => {
for (let i = 0; i < 10; i++) {
const newButton = document.createElement('button');
newButton.innerHTML = leaveButton.innerHTML;
newButton.style.top = Math.random() * window.innerHeight + 'px';
newButton.style.left = Math.random() * window.innerWidth + 'px';
newButton.setAttribute("id", "leave");
container.appendChild(newButton);
}
});
HTML code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>PressTheButton</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<button id="leave">Leave</button>
<h1>It's simple</h1>
<p>Just click the buttons</p>
</div>
<script src="script.js"></script>
</body>
</html>
答案1
得分: 1
不要回答我要翻译的问题。
以下是翻译的内容:
"Instead of adding the event listener to a fixed button you should work with delegated event listening. In the snippet below I added the click
event to the parent container
. Inside the event listener callback function I briefly check, wether the .textContent
of the clicked element is equal to "Leave" and, if not, I return immediately without carrying out any action.
<!-- begin snippet:js console:true -->
<!-- language:lang-css -->
button {position:absolute}
<!-- language:lang-js -->
const container = document.querySelector('.container');
const leaveButton = document.querySelector('#leave');
container.addEventListener('click', ev => {
if (ev.target.textContent !== "Leave") return;
for (let i = 0; i < 10; i++) {
const newButton = document.createElement('button');
newButton.innerHTML = leaveButton.innerHTML;
newButton.style top = Math.random() * window.innerHeight + 'px';
newButton.style left = Math.random() * window.innerWidth + 'px';
newButton.setAttribute("id", "leave");
container.appendChild(newButton);
}
});
<!-- language:lang-html -->
<head>
<meta charset="UTF-8">
<title>PressTheButton</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<button id="leave">Leave</button>
<h1>It's simple</h1>
<p>Just click the buttons</p>
</div>
<!-- end snippet -->
英文:
Instead of adding the event listener to a fixed button you should work with delegated event listening. In the snippet below I added the click
event to the parent container
. Inside the event listener callback function I briefly check, wether the .textContent
of the clicked element is equal to "Leave" and, if not, I return immediately without carrying out any action.
<!-- begin snippet:js console:true -->
<!-- language:lang-css -->
button {position:absolute}
<!-- language:lang-js -->
const container = document.querySelector('.container');
const leaveButton = document.querySelector('#leave');
container.addEventListener('click', ev => {
if (ev.target.textContent!=="Leave") return;
for (let i = 0; i < 10; i++) {
const newButton = document.createElement('button');
newButton.innerHTML = leaveButton.innerHTML;
newButton.style.top = Math.random() * window.innerHeight + 'px';
newButton.style.left = Math.random() * window.innerWidth + 'px';
newButton.setAttribute("id", "leave");
container.appendChild(newButton);
}
});
<!-- language:lang-html -->
<head>
<meta charset="UTF-8">
<title>PressTheButton</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<button id="leave">Leave</button>
<h1>It's simple</h1>
<p>Just click the buttons</p>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论