使用JavaScript删除HTML元素

时间:2016-07-14 16:19:56

标签: javascript html

我在向正在构建的表单中的元素添加删除按钮时遇到问题。最后一个函数deleteField()给了我麻烦。我尝试删除时不接受事件或颜色元素。

当它是节点类的子集时,为什么不接受元素作为节点?

    <script type="text/javascript">
var counter = 1;
function addInput(divName, isUpdate, eventId, color){
    var dropdown = document.createElement('select');
    var color_field = document.createElement('INPUT');
    var delete_button = document.createElement('BUTTON');
    var x  = document.createTextNode("X");
    delete_button.onclick = deleteField(divName, counter);

    color_field.setAttribute("type", "color");
    color_field.name = "color[" + counter + "]";
    color_field.id   = "c"+i;
    if(isUpdate) {
        color_field.value = color;
    }
    @eventList.map { event =>
        var option = document.createElement("option");
        option.value = "@event.getId";
        option.text  = "@event.getTitle";
        dropdown.add(option);}
        dropdown.name = "event[" + counter + "]";
        dropdown.id   = "e" + counter;
    color_field.appendChild(x);
    var doc = document.getElementById(divName);
    document.getElementById(divName).appendChild(document.createElement("br"));
    document.getElementById(divName).appendChild(document.createTextNode("Set event #" + counter));
    document.getElementById(divName).appendChild(dropdown);
    if(isUpdate){dropdown.value = eventId};
    document.getElementById(divName).appendChild(color_field);
    document.getElementbyId(divName).appendChild(delete_button);
    counter++;
    }

function setInput(divName){
    @existing_list.map { eventItem =>
        addInput(divName, true, @eventItem._1, '@eventItem._2.getOrElse("#000000")')
    }
}

    **function deleteField(divName, i){
        var parent = document.getElementById(divName);
        var event = document.getElementById("e"+i);
        var color = document.getElementById("c"+i);
        parent.removeChild(event);
        parentremoveChild(color);**

1 个答案:

答案 0 :(得分:1)

  

最后一个函数deleteField()给了我麻烦。我尝试删除时不接受事件或颜色元素。

底线parentremoveChild(color);应为parent.removeChild(color);