DOMNodeInserted和DOMNodeRemoved事件

时间:2015-11-12 13:26:15

标签: javascript dom dom-manipulation mutation-events dom-node

除了柜台外,下面的代码工作正常。当我向列表中添加项目时,计数器是准确的,但是当我从列表中删除项目时,它不会从计数器中减去一个项目。有任何想法吗?

HTML

<nav>
    <div id ="counter">1</div>
        <ul id="list">
            <li class="test" id="liOne"><a href="#">test</a></li>
        </ul>
        <p id="contOne"></p>

        <a id="button" class="button btn" href="#">Add</a><br>
        <a id="button" class="button removeBtn" href="#">Remove</a>
</nav>

的JavaScript

var elList, addLink, newEl, netText, counter, listItems, removeLink, removeEl;

elList = document.getElementById('list');
addLink = document.querySelector('.btn');
counter = document.getElementById('counter');
removeLink = document.querySelector('.removeBtn');

function addItem(e) {

if(e.preventDefault) {
    e.preventDefault();
} else {
    e.returnValue = false; //IE fallback code
}

newEl = document.createElement('li');
newText = document.createTextNode('New List Item');
newAnchor = document.createElement('a');
newAnchor.setAttribute('href', '#');
newAnchor.appendChild(newText);
newEl.appendChild(newAnchor);
elList.appendChild(newEl);

}   

function removeItem(e) {

if(e.preventDefault) {
    e.preventDefault();
} else {
    e.returnValue = false; //IE fallback code
}

var removeEl = document.querySelector('li');
var containEl = removeEl.parentNode;

containEl.removeChild(removeEl);

}

function updateCount() {                                 // Declare function
listItems = elList.getElementsByTagName('li').length;  // Get total of <li>s
counter.innerHTML = listItems;                         // Update counter
}

 removeLink.addEventListener('click', removeItem, false);
 addLink.addEventListener('click', addItem, false);
 elList.addEventListener('DOMNodeInserted', updateCount, false); // DOM                 updated. fires when a node is inserted into dom tree
 elList.addEventListener('DOMNodeRemoved', updateCount, false);

2 个答案:

答案 0 :(得分:2)

问题是updateCount()函数比removeItem()函数运行得更快(在删除项之前)。我建议:

删除以下行:

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

并按照以下方式致电updateCount() addItemremoveItem方法的结尾:

function removeItem(e) {

    /* Your code here*/

    updateCount();
}

编辑:查看 JSFiddle 示例

答案 1 :(得分:1)

突变事件have been deprecated(可以找到W3C的更多细节here)。相反,请使用mutation observers

DOM事件处理程序:

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

会改为以下内容。请注意,这将触发元素上所有DOM事件的updateCount,但观察者可以配置为处理特定事件。

var listObserver = new MutationObserver(updateCount);
listObserver.observe(elList, { attributes: true, childList: true, characterData: true });

下面是一个完整的,有效的例子。

&#13;
&#13;
var elList, addLink, newEl, netText, counter, listItems, removeLink, removeEl;

elList = document.getElementById('list');
addLink = document.querySelector('.btn');
counter = document.getElementById('counter');
removeLink = document.querySelector('.removeBtn');

function addItem(e) {

  if (e.preventDefault) {
    e.preventDefault();
  } else {
    e.returnValue = false; //IE fallback code
  }

  newEl = document.createElement('li');
  newText = document.createTextNode('New List Item');
  newAnchor = document.createElement('a');
  newAnchor.setAttribute('href', '#');
  newAnchor.appendChild(newText);
  newEl.appendChild(newAnchor);
  elList.appendChild(newEl);

}

function removeItem(e) {

  if (e.preventDefault) {
    e.preventDefault();
  } else {
    e.returnValue = false; //IE fallback code
  }

  var removeEl = document.querySelector('li');
  var containEl = removeEl.parentNode;

  containEl.removeChild(removeEl);

}

function updateCount() { // Declare function
  listItems = elList.getElementsByTagName('li').length; // Get total of <li>s
  counter.innerHTML = listItems; // Update counter
}


removeLink.addEventListener('click', removeItem, false);
addLink.addEventListener('click', addItem, false);

var listObserver = new MutationObserver(updateCount);
listObserver.observe(elList, { attributes: true, childList: true, characterData: true });
&#13;
<nav>
  <div id="counter">1</div>
  <ul id="list">
    <li class="test" id="liOne"><a href="#">test</a>
    </li>
  </ul>
  <p id="contOne"></p>


  <a id="button" class="button btn" href="#">Add</a>
  <br>
  <a id="button" class="button removeBtn" href="#">Remove</a>
</nav>
&#13;
&#13;
&#13;