gpt4 book ai didi

javascript - DOMNodeInserted 和 DOMNodeRemoved 事件

转载 作者:行者123 更新时间:2023-11-28 18:55:40 25 4
gpt4 key购买 nike

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

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

最佳答案

突变事件 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 });

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

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>

关于javascript - DOMNodeInserted 和 DOMNodeRemoved 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33672500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com