Javascript 如何从页面中删除特定div
我需要建立notes应用程序一样的网站,我必须删除特定的div点击x图标 我想我需要使用splice函数从数组中删除一个对象,但是我不能从数组中删除一个特定的对象Javascript 如何从页面中删除特定div,javascript,html,arrays,object,Javascript,Html,Arrays,Object,我需要建立notes应用程序一样的网站,我必须删除特定的div点击x图标 我想我需要使用splice函数从数组中删除一个对象,但是我不能从数组中删除一个特定的对象 var notesar=[] 函数objToArr(){ noteObj={ text:textAreaDv.value, 日期:dateInput.value, 时间:timeInput.value, } notesArr.push(noteObj) //console.log(notesar) printTohtml() } 函
var notesar=[]
函数objToArr(){
noteObj={
text:textAreaDv.value,
日期:dateInput.value,
时间:timeInput.value,
}
notesArr.push(noteObj)
//console.log(notesar)
printTohtml()
}
函数printTohtml(){
var str=“”
对于(i=0;i不能重用ID,请改用类
您可能需要执行noteId.innerHTML+=str
来连接
从
中删除onclick=“remove()”
,并向该
添加一个remove类
将索引添加为属性
str+=''
var notesar=[]
函数objToArr(){
noteObj={
text:textAreaDv.value,
日期:dateInput.value,
时间:timeInput.value,
}
notesArr.push(noteObj)
//console.log(notesar)
printTohtml()
}
函数printTohtml(){
var str=“”
对于(i=0;i
待办事项清单
发送
清楚的
如果您只想从屏幕上删除元素,而不需要对视图中剩余的元素排序,那么最简单的方法是使用节点。removeChild(child)
方法:
点击链接将其删除!
非常感谢。它工作得很好,但我没有真正了解你在那里做了什么。你能给我解释一下吗
// a click in the output div
document.getElementById("noteId").addEventListener("click",function(e) {
const tgt = e.target;
if (tgt.classList.contains("remove")) { // it was the remove that was clicked
notesArr.splice(tgt.dataset.idx,1); // take it IDX and remove the item
printTohtml(); // re-render
}
})