如何使用不可用的DOM对象运行函数?
我想运行一个函数以删除按钮的父元素。 问题后,父元素
let rem_display_btn1 = document.getElementById('rem_display_btn1');
与这样的
let rem_display_btn1 = document.getElementById('rem_display_btn1');
rem_display_btn1.addEventListener('click', () => {
let parentEl = rem_display_btn1.parentElement.parentElement;
parentEl.remove();
});
现在问题是脚本在HTML文件中加载的初始时刻,该按钮还没有。 也就是说: rem_display_btn1 是在运行另一个功能后产生的,并且该功能取决于未运行的 window.onload.onload
我还将在下面显示该功能,以防万一帮助:
// Cart list Table Element
let cart_list = document.getElementById('cart_list');
let numy = 0;
let htmlTR = '';
let cartUpdater = () => {
cart_list.innerHTML = "";
htmlTR = ''
numy = 0;
for(let a = 0; a < cart.length; a++) {
cart_list.innerHTML = "";
numy++;
htmlTR += `<tr id="htmlTR_ID${numy}">`;
htmlTR += `<td style="width: 7%;">${numy}</td>`;
htmlTR += `<td style="width: 25%;">${cart[a].name}</td>`;
htmlTR += `<td style="width: 25%;">${cart[a].price}</td>`;
htmlTR += `<td style="width: 15%; padding-left: 23px;"><button>-</button>1<button>+</button></td>`;
htmlTR += '</tr>';
console.log(htmlTR)
cart_list.innerHTML += htmlTR;
}
for(let b = 0; b < cart.length; b++) {
var remBtn_TD = document.createElement('td');
remBtn_TD.style.width = '25%';
var remBtn = document.createElement('button');
remBtn.classList.add('rem_btn_');
// Where the button element gets the **id: rem_display_btn1**
// so I'm choosing the first button that is produced
remBtn.id = `rem_display_btn rem_display_btn${b + 1}`;
remBtn.innerHTML = 'remove';
remBtn_TD.appendChild(remBtn);
console.log(cart_list.children[0].children[b]);
cart_list.children[0].children[b].appendChild(remBtn_TD);
}
}
我尝试在 cartupdater()函数之后添加if语句检查 document.getElementById('rem_display_btn1') ,然后才能将元素纳入可变但即使那样,我仍然
if (document.getElementById('rem_display_btn1')) {
let rem_display_btn1 = document.getElementById('rem_display_btn1');
rem_display_btn1.addEventListener('click', () => {
let parentEl = rem_display_btn1.parentElement.parentElement;
parentEl.remove();
});
} else {
console.log('Not There')
}
在想是否有一种方法可以使HTML/网页的当前状态总共获得,因此我也尝试将IF语句放在Settimeout和setInterval函数中,但它们都无法正常工作。问题是我确定这是一种工作的方式,我只是无法弄清楚。
谢谢。
添加setInterval函数:
setInterval(() => {
if (document.getElementById('rem_display_btn1')) {
let rem_display_btn1 = document.getElementById('rem_display_btn1');
rem_display_btn1.addEventListener('click', () => {
let parentEl = rem_display_btn1.parentElement.parentElement;
parentEl.remove();
});
} else {
console.log('Not There')
}
}, 4000);
对于SetteMeT:
setTimeout(() => {
if (document.getElementById('rem_display_btn1')) {
let rem_display_btn1 = document.getElementById('rem_display_btn1');
rem_display_btn1.addEventListener('click', () => {
let parentEl = rem_display_btn1.parentElement.parentElement;
parentEl.remove();
});
} else {
console.log('Not There')
}
}, 9000);
我在设置超时使用了更长的时间,因此我可以在获得rem_display_btn1按钮之前运行cartupdater()函数
I want to run a function to remove a button's parent element. parent element after getting it with
let rem_display_btn1 = document.getElementById('rem_display_btn1');
like this
let rem_display_btn1 = document.getElementById('rem_display_btn1');
rem_display_btn1.addEventListener('click', () => {
let parentEl = rem_display_btn1.parentElement.parentElement;
parentEl.remove();
});
The problem now is that the initial moment the script loads in the HTML file, the button is not yet there.
That is: rem_display_btn1 is produced after another function is run and that function it depends on those not run on window.onload
I will also display the function below in case it will be of help:
// Cart list Table Element
let cart_list = document.getElementById('cart_list');
let numy = 0;
let htmlTR = '';
let cartUpdater = () => {
cart_list.innerHTML = "";
htmlTR = ''
numy = 0;
for(let a = 0; a < cart.length; a++) {
cart_list.innerHTML = "";
numy++;
htmlTR += `<tr id="htmlTR_ID${numy}">`;
htmlTR += `<td style="width: 7%;">${numy}</td>`;
htmlTR += `<td style="width: 25%;">${cart[a].name}</td>`;
htmlTR += `<td style="width: 25%;">${cart[a].price}</td>`;
htmlTR += `<td style="width: 15%; padding-left: 23px;"><button>-</button>1<button>+</button></td>`;
htmlTR += '</tr>';
console.log(htmlTR)
cart_list.innerHTML += htmlTR;
}
for(let b = 0; b < cart.length; b++) {
var remBtn_TD = document.createElement('td');
remBtn_TD.style.width = '25%';
var remBtn = document.createElement('button');
remBtn.classList.add('rem_btn_');
// Where the button element gets the **id: rem_display_btn1**
// so I'm choosing the first button that is produced
remBtn.id = `rem_display_btn rem_display_btn${b + 1}`;
remBtn.innerHTML = 'remove';
remBtn_TD.appendChild(remBtn);
console.log(cart_list.children[0].children[b]);
cart_list.children[0].children[b].appendChild(remBtn_TD);
}
}
I've tried adding an if statement after the cartUpdater() function to check for document.getElementById('rem_display_btn1') before getting the element into a variable but even then it still doesn't exist
if (document.getElementById('rem_display_btn1')) {
let rem_display_btn1 = document.getElementById('rem_display_btn1');
rem_display_btn1.addEventListener('click', () => {
let parentEl = rem_display_btn1.parentElement.parentElement;
parentEl.remove();
});
} else {
console.log('Not There')
}
I'm thinking if there's a way to get the current state of the HTML/WebPage in total, so I also tried putting the if statement in a setTimeout and setInterval function but neither of them worked as well. The thing is I'm sure the is a way for this to work, I just can't figure it out.
Thanks in anticipation.
Adding the setInterval function:
setInterval(() => {
if (document.getElementById('rem_display_btn1')) {
let rem_display_btn1 = document.getElementById('rem_display_btn1');
rem_display_btn1.addEventListener('click', () => {
let parentEl = rem_display_btn1.parentElement.parentElement;
parentEl.remove();
});
} else {
console.log('Not There')
}
}, 4000);
For the setTimeout:
setTimeout(() => {
if (document.getElementById('rem_display_btn1')) {
let rem_display_btn1 = document.getElementById('rem_display_btn1');
rem_display_btn1.addEventListener('click', () => {
let parentEl = rem_display_btn1.parentElement.parentElement;
parentEl.remove();
});
} else {
console.log('Not There')
}
}, 9000);
I used a longer time for the set timeout so I can run the cartUpdater() function before I get the rem_display_btn1 button
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您根本不需要引用按钮的变量:
替代1(通过动态创建元素的代码添加事件处理程序)
如果要将事件处理程序附加到(即
rem_display_btn1
>)的元素被动态插入(即。最初加载文档后的一些未指定时间,请使用事件处理程序的HTML属性:替代2(听DOM更改)
另一个选择是响应添加到DOM中的元素。 Current DOM API implementations provide the
MutationObserver
接口支持此任务(注意:以前可用的事件domsubtreemprodified
,它也允许编写合适的处理程序也已被弃用)。You do not need the variable referencing the button at all:
Alternative 1 (Add event handler through the code that dynamically creates an element)
In case the element you want to attach the event handler to ( ie .
rem_display_btn1
) is inserted dynamically at some unspecified time after the document has initially been loaded, use the html attributes for event handlers:Alternative 2 (Listen to DOM changes)
Another option is to respond to elements added to the DOM. Current DOM API implementations provide the
MutationObserver
interface to support this task (Note: The formerly available eventDOMSubtreeModified
which would allow for writing a suitable handler too has been deprecated).您还可以将单击侦听器添加到任何立即存在的父元素(或整个
文档
),并检查单击元素是否匹配您的按钮的ID。这样,是否在稍后添加按钮都无关紧要。You can also add a click listener to any parent element that exists right away (or the entire
document
) and check if the clicked element matches your button's id. This way, it does not matter if the button is added at a later point.