如何修复removeLastQuestion()错误?

时间:2019-02-15 22:32:37

标签: javascript removechild

我是JS的新手,我尝试删除使用JS创建的最后一个div元素。但是总会出现此错误:无法在'Node'上执行'removeChild':参数1不是'Node'类型的问题是,如果我输入的问题数少于第一个输入的数字,我将删除问题数大于最后一个数字

<span>How many questions you want to add?</span>
<input type="text" id="qNum" >
<div id="containerBlocQ">`</div>
<button onclick="fieldAdd()">Add fields</button>
<button onclick="addNewQues()">Add a questions</button>
<button onclick="removeLastQuestion()">Remove a questions</button>

function fieldAdd(){
        let qNumber = document.getElementById("qNum").value;
        let containerBlocQ=document.getElementById('containerBlocQ');
        totalNumOfQuest = qNumber - nbQst;
        if(qNumber<=0){
            totalNumOfQuest=0;
        }
        if(totalNumOfQuest>=0){
            for(let i=1; i <= totalNumOfQuest ; i++){
                 addNewQues();
             }

        }
        else{
            for(i=0;Math.abs(totalNumOfQuest);i++){
                //Here is the error
                removeLastQuestion();
                nbQst--;
            }
        }


    }
    function removeLastQuestion(){
        nbQst--;
        let containerBlocQ = document.getElementById('containerBlocQ');
        containerBlocQ.removeChild(containerBlocQ.lastChild);
    }

0 个答案:

没有答案