Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.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对象没有方法';插入后';_Javascript_Html - Fatal编程技术网

Javascript 未捕获的typeerror对象没有方法';插入后';

Javascript 未捕获的typeerror对象没有方法';插入后';,javascript,html,Javascript,Html,当我单击按钮时,我试图在div元素中添加按钮。 以下JScode: function doThis(){ var btn= document.createElement("input"); btn.type="submit"; btn.value="button"; var child=document.getElementById("child"); var submit=document.getElementById("submit"); ch

当我单击按钮时,我试图在
div
元素中添加按钮。 以下
JS
code:

function doThis(){
    var btn= document.createElement("input");
    btn.type="submit";
    btn.value="button";
    var child=document.getElementById("child");
    var submit=document.getElementById("submit");
    child.insertAfter(btn, submit);
}
html
标记:

<div id="parent" class="parent">
    <div id="child" class="child">
        <input type="text" class="text"/>
        <input id="submit" type="submit" onclick="doThis()"/>
    </div>
<div>


在控制台中,我收到错误消息:
uncaughttypeerror:Object#没有方法insertafter
。如何修复此问题?

没有insertAfter,请改用insertBefore

 submit.parentNode.insertBefore(btn, submit.nextSibling);
如果submit是最后一个子节点,则submit.nextSibling为null,insertBefore将仅将其附加到parentNode


您可以使用
child.appendChild(btn)