function addExpense() {
let itemName = document.getElementById('item').value;
let priceOfItem = document.getElementById('price').value;
let tr = document.createElement('tr');
let td1 = tr.appendChild(document.createElement('td'));
let td2 = tr.appendChild(document.createElement('td'));
let td3 = tr.appendChild(document.createElement('td'));
td1.innerHTML = itemName;
td2.innerHTML = priceOfItem;
td3.innerHTML = '<input type="button" id="delete" value="delete" onclick="deleteExpense(this)">'
document.getElementById('expense_table').appendChild(tr);
}
function deleteExpense(thing) {
let deletion = thing.parentNode.parentNode;
deletion.parentNode.removeChild(deletion);
}
嘿,我的理解是,在我将按钮添加到单元格元素后,我调用delete函数并传递"this"(它自己的对象(作为参数,然后在delete函数本身中保留一个对象参数,并使用指向行然后指向表的parentNode属性?我走对了吗?
TLDR-我不明白下面的行是如何工作的,试着查找它,但没有找到正确的答案
let deletion = thing.parentNode.parentNode;
deletion.parentNode.removeChild(deletion);
thing是按钮,thing.parentNode是,thing.palentNode.parentNode是,deletion.parentNode是+Node.removeChild((–Andreas 1小时前