如何使新克隆的按钮与现有事件监听器一起工作?

huangapple go评论63阅读模式
英文:

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(&#39;.container&#39;);
const leaveButton = document.querySelector(&#39;#leave&#39;);

leaveButton.addEventListener(&#39;click&#39;, () =&gt; {
	for (let i = 0; i &lt; 10; i++) {
		const newButton = document.createElement(&#39;button&#39;);
		newButton.innerHTML = leaveButton.innerHTML;
		newButton.style.top = Math.random() * window.innerHeight + &#39;px&#39;;
		newButton.style.left = Math.random() * window.innerWidth + &#39;px&#39;;
		newButton.setAttribute(&quot;id&quot;, &quot;leave&quot;);
		container.appendChild(newButton);
	}
});

HTML code:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
	&lt;meta charset=&quot;UTF-8&quot;&gt;
	&lt;title&gt;PressTheButton&lt;/title&gt;
	&lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;div class=&quot;container&quot;&gt;
		&lt;button id=&quot;leave&quot;&gt;Leave&lt;/button&gt;
        &lt;h1&gt;It&#39;s simple&lt;/h1&gt;
        &lt;p&gt;Just click the buttons&lt;/p&gt;
	&lt;/div&gt;
	&lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

答案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 -->

huangapple
  • 本文由 发表于 2023年2月18日 20:36:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定