[英]Need a simple way to update the dynamic element database for JS to listen to events
當我想通過 JS 監聽動態創建的元素上的事件時,我需要借助監聽 body 上的事件來更新 JS 正在監聽的元素列表。 有沒有一種簡單優雅的方式來更新元素數據庫,讓 JS 在我想聽的時候聽?
document.querySelector('body').addEventListener('click', function (event) {
if (event.target.className.toLowerCase() === 'demo-classes') {
let closeBtns = document.getElementsByClassName("demo-classes");
Array.from(demoClasses).forEach(function (demoClass) {
demoClass.addEventListener("click", function () {
myArray.splice(demoClass.id.substring(2), 1);
render(myArray);
});
});
}
});
It leads to clicking two times, once on the body and another time on the element with the demo-classes
class before the splice
function removes the specific item from the arrays, then the render
function renders the html. 所有demo-classes
類都是使用createElement
生成的,並且類是使用render
function 中的setAttribute
設置的。
當您想要更新getElementsByClassName
function 中的項目數據庫時,使用location.reload()
刷新頁面。 將變量和動態元素存儲在 Session 存儲/本地存儲中,以便它在瀏覽器刷新后仍然存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.