带有动态DOM的Javascript get clicked元素
我在一张纸条上打呼噜,在纸条上我在桌子上创建了一组行。在最后一列()中,我添加了一个图标来删除该行。在创建行()时,我在图标中添加了一个调用函数的eventListner,到目前为止,在该函数上,我只在控制台上显示一条消息,以检查我是否正确捕获了事件,并且它工作正常。现在我尝试获取包含图标的父元素(),以便将其从表中删除 这是我现在的代码: 此函数用于创建新行:带有动态DOM的Javascript get clicked元素,javascript,html,dom,dom-manipulation,Javascript,Html,Dom,Dom Manipulation,我在一张纸条上打呼噜,在纸条上我在桌子上创建了一组行。在最后一列()中,我添加了一个图标来删除该行。在创建行()时,我在图标中添加了一个调用函数的eventListner,到目前为止,在该函数上,我只在控制台上显示一条消息,以检查我是否正确捕获了事件,并且它工作正常。现在我尝试获取包含图标的父元素(),以便将其从表中删除 这是我现在的代码: 此函数用于创建新行: const addProductCart = () => { const cartRow = document.create
const addProductCart = () => {
const cartRow = document.createElement('tr');
cartRow.classList.add("itemRow");
const productColumn = document.createElement('td');
productColumn.innerHTML = optionList.options[optionList.selectedIndex].text;
cartRow.appendChild(productColumn);
const unitsColumn = document.createElement('td');
unitsColumn.innerHTML = units.value;
cartRow.appendChild(unitsColumn);
const unitPrice = document.createElement('td');
unitPrice.innerHTML = optionList.value;
cartRow.appendChild(unitPrice);
const totalPrice = document.createElement('td');
totalPrice.innerHTML = optionList.value * units.value;
cartRow.appendChild(totalPrice);
const removeButton = document.createElement('img');
removeButton.setAttribute("src", "data/icons/detele1.svg");
removeButton.classList.add("icon");
removeButton.addEventListener('click', eraseProduct);
cartRow.appendChild(removeButton);
carritoFila.appendChild(cartRow);
}
每次单击“擦除”按钮时,都会启动此功能:
const eraseProduct = () => {
console.log(`clic`);
}
使用编辑器中的
按钮创建。const-eraseProduct=(事件)=>{console.log(event.target.parentNode);}