Javascript 为什么.addEventListener()不工作而.onClick()不工作?
我有一个js文件,可以导入index.html。在js文件中,我根据另一个按钮单击动态创建按钮Javascript 为什么.addEventListener()不工作而.onClick()不工作?,javascript,html,Javascript,Html,我有一个js文件,可以导入index.html。在js文件中,我根据另一个按钮单击动态创建按钮 var removeButton = document.createElement("button"); removeButton.appendChild(document.createTextNode("X")); removeButton.setAttribute( "onClick"
var removeButton = document.createElement("button");
removeButton.appendChild(document.createTextNode("X"));
removeButton.setAttribute(
"onClick",
'remove("' + ann + '","' + lastid + '")'
);
但是删除功能只有在我将其放入索引文件中时才起作用。如果我这样做
removeButton.addEventListener("click",function(e){
remove(ann,lastid)
})
并在js文件中定义remove,当我点击按钮时会出现以下错误:
未捕获的TypeError:未能在“节点”上执行“removeChild”:参数1不是“节点”类型
删除功能:
function remove(ann, id) {
var list = document.getElementById("list");
var item = document.getElementById(id);
list.removeChild(item); // this gives error
....
}
};
知道为什么吗
编辑:添加了lastid,已定义列表(复制/粘贴错误,抱歉)在删除功能中,未定义列表。这就是错误的原因,方法是确保定义了最后一个。。。工作版本使用
lastid
您在这里使用jquery吗?