1

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

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);
4

2 回答 2

3

突变事件已被弃用(可以在此处找到 W3C 的更多细节)。相反,使用突变观察者

DOM 事件处理程序:

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

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

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

下面是一个完整的工作示例。

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 });
<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>

于 2015-11-12T13:49:46.323 回答
1

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

删除以下行:

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

并调用你的 a和方法updateCount()的结尾,如下所示:addItemremoveItem

function removeItem(e) {

    /* Your code here*/

    updateCount();
}

编辑:请参阅工作JSFiddle示例

于 2015-11-12T13:41:45.993 回答