简体   繁体   English

Javascript动态添加元素

[英]Javascript dynamically add elements

I'm trying to dynamically add table rows using Javascript (I will add Ajax requests later on), but I'm having the following error: Uncaught Error: NOT_FOUND_ERR: DOM Exception 8 我试图使用Javascript动态添加表行(稍后我将添加Ajax请求),但是出现以下错误: Uncaught Error: NOT_FOUND_ERR: DOM Exception 8

My code is as follows: 我的代码如下:

function SplitsJob() {

var newJob = document.createElement("tr");
newJob.innerHTML = "<tr><td>Foo</td><td>Bar</td></tr>";

var under = document.getElementById("row1");
document.body.insertBefore(newJob, under);
}

When this function is called, I want to add another <tr> (with the contents of newJob.innerHTML ) beneath the <tr> with the id row1 . 调用此函数时,我想在ID为row1<tr>下添加另一个<tr> (具有newJob.innerHTML的内容)。

I have found the code on this Mozilla page 我在此Mozilla页面上找到了代码

sample HTML: 示例HTML:

<table id="mytable">
    <tbody>
        <tr id="row1"><td>xxx</td><td>Bar</td></tr>
    </tbody>
</table>
<a href="#" onclick="javascript:InsertBefore();return false;">Insert Before</a><br/>
<a href="#" onclick="javascript:AppendChild();return false;">Append Child</a><br/>
<a href="#" onclick="javascript:InsertRow();return false;">InsertRow</a>

samle SCRIPT: 样本脚本:

var i=0;
function randColor() {
    var str=Math.round(16777215*Math.random()).toString(16);
    return "#000000".substr(0,7-str.length)+str;
}

function InsertBefore() {
    var table = document.getElementById("mytable");
    var under = document.getElementById("row1");
    var newJob = document.createElement("tr");
    newJob.style.backgroundColor=randColor();
    //newJob.innerHTML = "<tr><td>Foo</td><td>Bar</td></tr>"; // its inserted inside TR. no additional TR's needed.
    newJob.innerHTML = "<td>Foo "+(i++)+".</td><td>Bar</td>";
    table.tBodies[0].insertBefore(newJob,under);
}

function AppendChild() {
    var table = document.getElementById("mytable");
    var newJob = document.createElement("tr");
    newJob.style.backgroundColor=randColor();
    newJob.innerHTML = "<td>Foo "+(i++)+".</td><td>Bar</td>";
    table.tBodies[0].appendChild(newJob);
}


function InsertRow() {
    var indexToInsert=1;
    var table = document.getElementById("mytable");
    var newJob = table.tBodies[0].insertRow(indexToInsert);
    newJob.style.backgroundColor=randColor();
    newJob.innerHTML = "<td>Foo "+(i++)+".</td><td>Bar</td>";
}

TR is a "table row". TR是“表格行”。 TR-elements can be appended only into TABLE, TBODY, THEAD, TFOOT elements. TR元素只能附加到TABLE,TBODY,THEAD,TFOOT元素中。

Find for appendChild() and insertRow() methods in MDN and MSDN 在MDN和MSDN中查找appendChild()insertRow()方法

Use jQuery for this porposes. 为此使用jQuery。 It's mutch simplier, less code and cross-browser. 它更简单,代码更少,浏览器更简单。 Here is examples . 这是例子

$('<tr>').html('<td>Foo2</td><td>Bar</td>').insertAfter('#row1');

Rather than calling insertBefore on the document.body, try calling it on the table: 与其在document.body上调用insertBefore,不如在表上调用它:

var myTable = document.getElementsByTagName("table")[0]; // this is assuming you have just one table

myTable.insertBefore(newJob, under);

Also, since you're creating a tr element, don't put the tr in the innerHTML, just put what will go inside it. 另外,由于您正在创建tr元素,因此请勿将tr放入innerHTML中,而只需将要放入的内容放入其中。

var newJob = document.createElement("daps");

daps is not a valid tag. daps不是有效的标签。 You're not supposed to pass in the id for the tag you want, but the element, eg 您不应该传递所需标签的ID,而是传递元素,例如

newDiv = document.createElement("div");

Your current code is trying to create a "daps" element (whatever that is). 您当前的代码正在尝试创建一个“小睡”元素(无论是什么)。 You need to specify the tag name, not an Id or other value. 您需要指定标签名称,而不是ID或其他值。 Try this: 尝试这个:

var newJob = document.createElement("tr");
newJob.innerHTML = "<td>Foo</td><td>Bar</td>";

** Edited ** It seems that without hacking it is impossible to add elements to table (makes sense in a way). **编辑**看来,如果不进行黑客攻击,就不可能在表中添加元素(从某种意义上讲是合理的)。 Therefore you have to use dedicated insertRow and insertCell functions; 因此,您必须使用专用的insertRowinsertCell函数。

function SplitsJob() {
    var table = document.getElementById("table1");
    var newRow = table.insertRow(0);

    var newCell = newRow.insertCell(0);
    newCell.innerHTML = "Bar";

    newCell = newRow.insertCell(0);
    newCell.innerHTML = "Foo";
}

Note that in this example new row will be created at the top of table and Foo will be before Bar 请注意,在此示例中,新行将表格顶部创建而Foo将 Bar 之前。

Uncaught Error: NOT_FOUND_ERR: DOM Exception 8 means that the parent element ( document.body ) does not contain the element you are searching for ( under ). Uncaught Error: NOT_FOUND_ERR: DOM Exception 8意味着父元素( document.body )不包含你正在寻找(元素under )。

ie your under element is not directly under the body tag. 即您的under元素不是直接在body标签下面。

Try doing - 尝试做-

document.body.insertBefore(newJob, newJob.parent);

And of course, innerHTML should not contain the tag itself! 当然, innerHTML不应包含标签本身! So do - 也是-

newJob.innerHTML = "<td>Foo</td><td>Bar</td>";

Perhaps a typo? 也许是错字? Does it work in other browser then Chrome? 是否可以在其他浏览器(而不是Chrome)中使用?

NOT_FOUND_ERR code 8 The referenced node does not exist; NOT_FOUND_ERR代码8所引用的节点不存在;必须为0。 for example using insertBefore with a child node that is not a child of the reference node. 例如,将insertBefore与不是参考节点的子节点的子节点一起使用。

See http://observercentral.net/exception8 & http://reference.sitepoint.com/javascript/DOMException 参见http://observercentral.net/exception8http://reference.sitepoint.com/javascript/DOMException

Also, if you just want to add rows to the table, use appendChild() instead of insertBefore(). 另外,如果只想向表中添加行,请使用appendChild()而不是insertBefore()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM