gpt4 book ai didi

javascript - 从表中删除项目

转载 作者:行者123 更新时间:2023-11-30 21:04:24 25 4
gpt4 key购买 nike

我正在解决这个问题,我想使用 javascript 在 HTML 表格中添加和删除项目。到目前为止我已经知道了,但我真的不知道如何使删除部分成为可能。你能帮我一下吗?

let inputJmeno = document.querySelector('#inputJmeno');
let inputPrijmeni = document.querySelector('#inputPrijmeni');
let inputVek = document.querySelector('#inputVek');
let buttonAdd = document.querySelector('#add');

let table = document.querySelector('table');
let tbody = document.querySelector('tbody');
let tr = document.querySelector('tr');

let jmeno = null;
let prijmeni = null;
let vek = null;
let pocetOsob = 0;

buttonAdd.addEventListener('click', add);

function add() {
jmeno = inputJmeno.value;
prijmeni = inputPrijmeni.value;
vek = inputVek.value;

let newRow = document.createElement('tr');
let newJmeno = document.createElement('td');
let newPrijmeni = document.createElement('td');
let newVek = document.createElement('td');
let krizek = document.createElement('span');

krizek.id = "krizek" + pocetOsob;
krizek.className = "krizClass";
newRow.id = "row" + pocetOsob;

newJmeno.innerHTML = jmeno;
newPrijmeni.innerHTML = prijmeni;
newVek.innerHTML = vek;
krizek.innerHTML = 'x';

tbody.appendChild(newRow);
newRow.appendChild(newJmeno);
newRow.appendChild(newPrijmeni);
newRow.appendChild(newVek);
newRow.appendChild(krizek);

load(pocetOsob);
pocetOsob++;
}

function load(p) {
let krz = document.querySelector('#krizek'+p);
console.log(p);
}

最佳答案

尝试

newRow.removeChild(krizek);

关于javascript - 从表中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46797028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com