[英]How to delete a JSON object with the information of the DOM (or by id)?
我正在嘗試刪除帶有表信息的 JSON 對象。
首先我聲明對象為空:
let notas = [];
然后我通過用戶交互操作它,並使用獲取的信息創建一個table
。 在其中一個單元格中,我創建了一個用於訪問該行的按鈕:
function añadirNota() {
//DOING THINGS TO COLLECT DATA
const notaObj = {
id: Date.now(),
dh: tiempo(),
imp: colorPArray,
texto: nota
}
notas = [...notas, notaObj];
//CREATING TABLE AND BUTTON
var table = document.getElementById("tablanotas");
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
//INSERT THE BUTTON WITH THE deleteRow() function
var textoTransformado = notaObj.texto + `<button class='btn' onclick="deleteRow(this)">❌</button>`;
cell1.innerHTML = notaObj.dh;
cell2.innerHTML = importancia;
cell3.innerHTML = textoTransformado;
}
function deleteRow(r) {
var h = r.parentNode.parentNode.rowIndex;
document.getElementById("tablanotas").deleteRow(h);
}
現在的問題是,我嘗試將其插入到deleteRow()
函數中,以便與對象進行比較:
var t = document.getElementById("tablanotas"),
d = t.getElementsByTagName("td")[0];
但我不知道接下來會發生什么。 我可以在一個對象內迭代,與td
元素進行比較,如果它們相等,刪除該對象嗎?
在標簽中使用 ID ("someId") 比使用 getElementById 函數檢索標簽信息更容易:
var textoTransformado = notaObj.texto + `<button id="someId" class='btn' onclick="deleteRow(this)">❌</button>`;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.