我不得不召回自我調用的方法,將事件偵聽器添加到新添加的元素列表如何在其他函數中調用自調用javascript函數?
var textPop = document.getElementById('textPopper');
var elements = document.getElementById('our-list');
var reloader = (function reload() {
document.querySelectorAll('ul li').forEach(function(click) {
click.addEventListener("click", function() {
textPop.innerHTML = this.innerHTML;
})
});
}
)();
function addItem() {
elements.innerHTML += "<li> Something new stuff </li>";
reloader(); //getting error here (Uncaught TypeError: again is not a function)
}
<h3 id="textPopper">Text popper </h3>
<ul id="our-list">
<li>First item </li>
<li>Second item</li>
<li>Third item</li>
<li>Fourth item</li>
</ul>
<script src="my.js"></script>
你得到的錯誤是什麼?在問題中添加所有細節是很好的。 – epascarello