这是我的ajax代码,可以使用删除按钮动态插入div。
$('#diplayContainerDiv').append('<div id="divQueryDisplay'+ divCounter +'">' +val1+val2+val3+ '</div><div id="removeButtonDiv"><input type="button" name="removeButton" id="removeButton'+ removeButtonCounter +'" value="Remove" onclick="removeRemoveButtonOnClick();" ></div>');
我试图在删除按钮
上调用以下功能 function removeRemoveButtonOnClick(){
//document.getElementById('#divQueryDisplay'+removeButtonCounter).innerHTML =""; // this does not works
var parentElement=document.getElementById('#diplayContainerDiv');
var childElement= document.getElementById('#divQueryDisplay'+removeButtonCounter);
childElement.parentElement.removeChild(childElement);
}
我收到以下错误:
Uncaught TypeError: Cannot read property 'parentElement' of null
at removeRemoveButtonOnClick
at HTMLInputElement.onclick
我哪里错了?
答案 0 :(得分:1)
getElementById
将元素id作为参数,而不是id选择器
var parentElement=document.getElementById('diplayContainerDiv');
var childElement= document.getElementById('divQueryDisplay'+removeButtonCounter);
删除#