为新添加的元素在纯JS中添加点击事件。

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

Add click events to newly added elements in pure JS

问题

我想要为具有类名 item 的元素添加点击事件。在现有元素上运行良好:

const enlargables = document.querySelectorAll('.item');
enlargables.forEach(function(el) {
    el.addEventListener('click', function(e) {
        alert('hello');
    })
});

但是,如果元素在页面加载后动态添加,事件将不会绑定到这些新元素上。

如何使用纯JS将事件添加到新添加的具有类名 item 的元素?类似于jQuery中的document.ready的工作方式。

英文:

I want to add a click event to elements with class item. Work fine:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const enlargables = document.querySelectorAll(&#39;.item&#39;);
enlargables.forEach(function(el) {
    el.addEventListener(&#39;click&#39;, function(e) {
        alert(&#39;hello&#39;);
    })
});

<!-- language: lang-html -->

&lt;div class=&quot;item&quot;&gt;test 1&lt;/div&gt;
&lt;div class=&quot;item&quot;&gt;test 2&lt;/div&gt;

<!-- end snippet -->

But if the element is added dynamically after pageload, the event will not be added to the element.

How can I add the event to newly added elements with class item using pure JS? Similar to how document ready works in jQuery.

答案1

得分: 1

这是发生的原因是因为您的 .item 元素是动态创建的。换句话说,它是在您的侦听器已分配后稍后附加到 DOM 的。甚至应该使用委派来实现这一点

JavaScript:

document.addEventListener("click", function(e){
   const hasClass = event.target.classList.contains('item');
   if(hasClass) {
    //在这里添加您的逻辑
   }
});

jQuery:

$('body').on('click', '.item', (event) => {
   //在这里添加您的逻辑
});
英文:

This is happening because your .item element is dynamically created. In other words, it is attached to the DOM later after your listeners are already assigned. Even delegation should be used to achieve this

JavaScript:

document.addEventListener(&quot;click&quot;, function(e){
   const hasClass = event.target.classList.contains(&#39;item&#39;);
   if(hasClass) {
    //Add your logic here
   }
});

jQuery:

$(&#39;body&#39;).on(&#39;click&#39;, &#39;.item&#39;, (event) =&gt; {
   //Add your logic here
});

答案2

得分: 1

你可以在最近的静态祖先上使用事件代理。

英文:

You can use event delegation on the nearest static ancestor.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// document only used for demonstration
document.addEventListener(&#39;click&#39;, e =&gt; {
  // use .closest instead to handle clicks on the descendants of .item
  if (e.target.matches(&#39;.item&#39;)) console.log(&#39;click&#39;);
});

document.querySelector(&#39;button&#39;).addEventListener(&#39;click&#39;, function(e) {
  const newItem = document.createElement(&#39;div&#39;);
  newItem.classList.add(&#39;item&#39;);
  newItem.textContent = &#39;test&#39;;
  this.before(newItem);
});

<!-- language: lang-html -->

&lt;div class=&quot;item&quot;&gt;test 1&lt;/div&gt;
&lt;div class=&quot;item&quot;&gt;test 2&lt;/div&gt;
&lt;button&gt;Add&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月10日 21:45:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977665.html
匿名

发表评论

匿名网友

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

确定