【问题标题】:Added element prevents code from adding new rows to table [closed]添加的元素可防止代码向表中添加新行 [关闭]
【发布时间】:2013-02-12 16:49:09
【问题描述】:

我在 div 标签中向表添加行时遇到了一些问题,因为其中有另一个标签。

我知道如果我可以更改表的 id 会很好,但是因为它是生成的,所以我无法更改它们。我只能更改包含表格的 div 的 id。

<!--Adding does not work on this table -->
<h3>Wednesday</h3>
<button type="button" onclick="displayResult('wed')">Insert new row</button>
<div id="wed">
<strong>Even this stops it from adding</strong> <!--This is the problem -->
<table id="myTable" border="1">
  <tr>
    <td>cell 1</td>
    <td>cell 2</td>
  </tr>
</table>
<div>   

<!--Adding works on this table -->
<h3>Thursday</h3>
<button type="button" onclick="displayResult('thu')">Insert new row</button>
<div id="thu">
<table id="myTable" border="1">
  <tr>
    <td>cell 1</td>
    <td>cell 2</td>
  </tr>
</table>
<div>   

function displayResult(number) {
    var elemList=document.getElementById(number).childNodes;
    var table=elemList[1];
    var row=table.insertRow(-1);
    var cell1=row.insertCell(0);
    var cell2=row.insertCell(1);
    cell1.innerHTML="New";
    cell2.innerHTML="New";
}

Here is jsFiddle illustrating the problem I have

【问题讨论】:

  • 混淆缩短器的背后是什么?请充实您的问题。
  • 很高兴在这里看到一些,但链接中的小提琴很清楚有什么问题。
  • @krillgar, we like a question to contain all the content 这样,例如,如果链接坏了,问题及其答案对未来的访问者仍然有用。
  • 点了,虽然这仍然不能保证否决票。不过,您的评论比 gdoron 的更好,更有建设性。
  • 我做了更改.. 有人也可以解封我吗? :(

标签: javascript html asp.net-web-api


【解决方案1】:

我会假设通过索引获取元素是问题所在(没有看到生成表格的代码,很难确定),并建议一旦你有了 div,你 getElementsByTagName('table')[0]看看你是否不能从那里工作。

我还建议您使用 jquery 做的大部分事情都容易得多,如果您有能力将其添加到项目中,我会推荐它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    • 2016-07-23
    • 2017-11-02
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多