Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.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 insertAdjacentElement工作不正常_Javascript_Google Chrome_Internet Explorer - Fatal编程技术网

javascript insertAdjacentElement工作不正常

javascript insertAdjacentElement工作不正常,javascript,google-chrome,internet-explorer,Javascript,Google Chrome,Internet Explorer,在我的javascript代码中,使用了InsertAdjaceEntelement。 它在IE中工作得很好,但在chrome中工作得很好,但不是以预期的方式。它会在其他地方插入新的html。所以我想用另一种方法。我以前也试过使用appendChild和Insert,但没有成功 这是我的代码行 newPage = nd.insertAdjacentElement("beforeBegin", oNewItem); 谢谢我相信也会是这样 newPage = nd.parentElement.in

在我的javascript代码中,使用了InsertAdjaceEntelement。 它在IE中工作得很好,但在chrome中工作得很好,但不是以预期的方式。它会在其他地方插入新的html。所以我想用另一种方法。我以前也试过使用appendChild和Insert,但没有成功

这是我的代码行

newPage = nd.insertAdjacentElement("beforeBegin", oNewItem);

谢谢

我相信也会是这样

newPage = nd.parentElement.insertBefore(oNewItem, nd);
您可以在这里尝试:

在未正确使用之前,应在JavaScript DOM insertBefore中更改标题

这是因为,即使没有放入DOM,浏览器也会自动创建一个元素来包装表中的行。因此,formtable不是submitrow的父级,这就是为什么会出现错误

试试这个:

formtable.tBodies[0].insertBefore(new_tr, submitrow);
或者,更一般地说:

submitrow.parentNode.insertBefore(new_tr, submitrow);
在支持它的浏览器中,甚至有这样一个:

submitrow.insertAdjacentElement("beforeBegin", new_tr);
Internet Explorer、Chrome、Safari和Opera都支持它,Firefox不支持,但它可以很容易地升级。无论如何,我不推荐使用这种东西

并且,作为一般建议,在写表格时始终使用标签:

<table id="formtable">
    <tbody>
        <tr>
            <td>Number : </td>
            <td><input type="text" name="num[]" /></td>
        </tr>
        <tr id="submitrow">
            <td><input type="button" value="Add one more" onclick="addRow()" /></td>
            <td><input type="submit" value="Go!" /></td>
        </tr>
    </tbody>
</table>

当然,当你需要的时候

你能解释一下你预期会发生什么,也许是你在代码中使用的newPage,nd,oNewItem变量吗?我有4,5帧。每次点击它都会生成新的divpage,并通过iframe进入新创建的页面。但在chrome中,它会创建新页面,但总是将iframe html粘贴到第一个divpage中。这听起来可能是由于代码的另一部分出现了问题。你能用更多的javascript更新这个问题吗?这样上下文就更清楚了。这个问题似乎与IE中的问题无关-在IET中它工作得很好。这个问题似乎也与表无关?你能解释一下结果是什么吗,和你期望的有什么不同?我有4,5帧。每次点击它都会生成新的divpage,并通过iframe进入新创建的页面。但在chrome中,它会创建新页面,但总是将iframe html粘贴到第一个divpage中。