这是我的 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
我哪里出错了?
最佳答案
getElementById
将元素 id 作为参数,而不是 id 选择器
var parentElement=document.getElementById('diplayContainerDiv');
var childElement= document.getElementById('divQueryDisplay'+removeButtonCounter);
删除#
关于javascript - 从子div中获取父div的内容并调用子div中按钮上的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49298840/