简体   繁体   English

当 DOMNodeInserted 事件触发时, upateCount function 会按预期工作,但当 DOMNodeRemoved 触发时不会再次针对相同的 function?

[英]When DOMNodeInserted event is fire upadateCount function works as expected but not when DOMNodeRemoved is fire which again target the same function?

HTML HTML

here is the left and right button in HTML to remove list items and add list items respectively.这里是 HTML 中的左右按钮,分别用于删除列表项和添加列表项。

<div id="groceries">
           <h1>Buy Groceries<span id="counter">0</span></h1>
           <ul id="list"></ul>

           <div class="left button"><a  id ="lLink" href="">REMOVELISTITEM</a></div>     
           <div class="right button"><a  id ="rLink" href="">ADDLISTITEM</a></div>
         
           <div class="clear"></div>
      </div>

java script java 脚本

as click event fire on thise <div class="button left"><a id ="lLink" href="">REMOVELISTITEM</a></div> and thise <div class="button right"><a id ="rLink" href="">ADDLISTITEM</a></div> buttons list item removed or add into the list because of which Mutation events fire当点击事件触发 thise <div class="button left"><a id ="lLink" href="">REMOVELISTITEM</a></div>和 thise <div class="button right"><a id ="rLink" href="">ADDLISTITEM</a></div>按钮列表项已删除或添加到列表中,因此会触发 Mutation 事件

Elist.addEventListener('DOMNodeInserted',updateCount,false);
Elist.addEventListener('DOMNodeRemoved',updateCount,false);

which targets the same "updateCount" function but when the "DOMNodeInserted" event fire it calculate the right number of list items but not when "DOMNoderemoved" event fire why?它以相同的“updateCount” function 为目标,但是当“DOMNodeInserted”事件触发时,它会计算正确的列表项数量,但在“DOMNoderemoved”事件触发时却不计算为什么?

  function addListItem(e){
  e.preventDefault();
  newListnode = document.createElement('li');
  textNode = document.createTextNode("new list item");
  newListnode.appendChild(textNode);
  Elist.appendChild(newListnode);
}

function removeListItem(e){
    e.preventDefault();
    Elist.removeChild(Elist.lastElementChild);
}

function updateCount(e){
  
    var numOfItems = Elist.getElementsByTagName('li').length;
    Ecounter.innerHTML = numOfItems;
}

ElLink.addEventListener('click',removeListItem,false);
ErLink.addEventListener('click',addListItem,false);

Elist.addEventListener('DOMNodeInserted',updateCount,false);
Elist.addEventListener('DOMNodeRemoved',updateCount,false);

Adding or Removing Items to/from DOM can take a little bit of time and therefore you do not have the results immediately.在 DOM 中添加或删除项目可能需要一点时间,因此您不会立即获得结果。 With this in mind, you should wait for the DOM to update, then do your job.考虑到这一点,您应该等待 DOM 更新,然后完成您的工作。 putting a simple setTimeout() would put your code in a lower order in eventCycle and can solve your issue.放置一个简单的setTimeout()将使您的代码在eventCycle中的顺序较低,并且可以解决您的问题。

By the way, this is not the best approach tho, you can get the idea of what's happening and use a more advanced or optimized way to solve it顺便说一句,这不是最好的方法,您可以了解正在发生的事情并使用更高级或优化的方法来解决它

 var Elist = document.getElementById('list'); var ElLink = document.getElementById('lLink'); var ErLink = document.getElementById('rLink'); var Ecounter = document.getElementById('counter'); function addListItem(e) { e.preventDefault(); newListnode = document.createElement('li'); textNode = document.createTextNode("new list item"); newListnode.appendChild(textNode); Elist.appendChild(newListnode); } function removeListItem(e) { e.preventDefault(); Elist.removeChild(Elist.lastElementChild); } function updateCount(e) { setTimeout(function() { var numOfItems = Elist.getElementsByTagName('li').length; Ecounter.innerHTML = numOfItems; }, 0); } ElLink.addEventListener('click', removeListItem, false); ErLink.addEventListener('click', addListItem, false); Elist.addEventListener('DOMNodeInserted', updateCount, false); Elist.addEventListener('DOMNodeRemoved', updateCount, true);
 <div id="groceries"> <h1>Buy Groceries<span id="counter">0</span></h1> <ul id="list"></ul> <div class="left button"><a id="lLink" href="">REMOVELISTITEM</a></div> <div class="right button"><a id="rLink" href="">ADDLISTITEM</a></div> <div class="clear"></div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM