Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:无法读取属性';parentElement';在.remove()上未定义的_Javascript_Html - Fatal编程技术网

Javascript TypeError:无法读取属性';parentElement';在.remove()上未定义的

Javascript TypeError:无法读取属性';parentElement';在.remove()上未定义的,javascript,html,Javascript,Html,我用这个函数动态创建按钮 function addFormFields() { i += 1 const parentdiv = document.createElement('div') const delicon = document.createElement('i') const btndelete = document.createElement('button') const div = document

我用这个函数动态创建按钮

function addFormFields() {
        i += 1
        const parentdiv = document.createElement('div')
        const delicon = document.createElement('i')
        const btndelete = document.createElement('button')
        const div = document.createElement('div')
        const container = document.getElementById('compinput')
        const selectprod = document.createElement('select')
        const inputqtd = document.createElement('input')
        delicon.setAttribute("class", "fas fa-trash-alt")
        btndelete.setAttribute("class", "btn btn-danger btn-sm")
        btndelete.setAttribute("type", "button")
        btndelete.setAttribute("onclick", "deletebtn()")
        div.setAttribute("class", "row justify-content-between col-md-12")

        inputqtd.setAttribute("class", "form-control col-md-4 mt-1")
        inputqtd.setAttribute("min", "0")
        inputqtd.setAttribute("max", "999")
        inputqtd.type = "number"
        inputqtd.name = "qtd[]"
        inputqtd.placeholder = " Quantidade"



        selectprod.setAttribute("class", "form-control col-md-6")
        selectprod.type = "text"
        selectprod.name = "prod[]"
        selectprod.placeholder = "Produtos que compoem"


        console.log(lista_prod)

        for (let i = 0; i < lista_prod.length; i++) {
            const opt = document.createElement('option');
            opt.value = lista_prod[i];
            opt.innerHTML = lista_prodname[i]
            selectprod.appendChild(opt)

        }

        btndelete.appendChild(delicon)
        div.appendChild(selectprod)
        div.appendChild(btndelete)
        parentdiv.appendChild(div)
        parentdiv.appendChild(inputqtd)
        container.appendChild(parentdiv)

        container.appendChild(document.createElement("br"))
    }

<> >应该发生什么:删除按钮<代码> BTNeleDe>/Cult>应该代码> OnCase< /Cord>删除他动态创建的父元素,它被称为<代码> PARTENDIV,但是当我尝试删除时,我收到的错误像问题的标题一样,所以它不考虑动态创建的DIV作为他的父元素,我该怎么做呢

在同一作用域中已经有parentdiv和delete按钮。为什么不这样添加事件处理程序:

btndelete.addEventListener("click",() => {
   container.removeChild(parentdiv);
});

deletebtn(this)
btndelete.setAttribute(“onclick”、“deletebtn(this)”)
运行代码并确认

var i=1;
var lista_prod=[1,2];
变量lista_prodname=[“dd”,“ddd3”]
函数addFormFields(){
i+=1
const parentdiv=document.createElement('div')
const delicon=document.createElement('i')
const btndelete=document.createElement('按钮')
const div=document.createElement('div')
const container=document.getElementById('compinput')
const selectprod=document.createElement('select')
const inputqtd=document.createElement('input')
delicon.setAttribute(“类”、“fas-fa-trash-alt”)
setAttribute(“类”,“btn btn危险btn sm”)
setAttribute(“类型”、“按钮”)
btndelete.innerHTML='delete';
setAttribute(“onclick”、“deletebtn(this)”)
div.setAttribute(“类”,“行对齐col-md-12之间的内容”)
inputqtd.setAttribute(“类”,“窗体控件col-md-4 mt-1”)
inputqtd.setAttribute(“最小”、“0”)
inputqtd.setAttribute(“最大”、“999”)
inputqtd.type=“编号”
inputqtd.name=“qtd[]”
inputqtd.placeholder=“量化数据”
选择prod.setAttribute(“类”、“窗体控件col-md-6”)
选择prod.type=“text”
选择prod.name=“prod[]”
选择prod.placeholder=“Produtos que componem”
for(设i=0;i

添加非常简单的

函数deletebtn(e){
e、 target.parentElement.remove()
}

btndelete.setAttribute(“onclick”,“deletebtn(this)”)只需要少量的更正,而不是您的代码是正确的
onclick
侦听器无法传递事件。
btndelete.addEventListener("click",() => {
   container.removeChild(parentdiv);
});