在表中动态插入新行

发布于 2024-09-06 00:22:28 字数 1228 浏览 2 评论 0原文

<script type="text/javascript" language="javascript">
 function addNewRow()
 {
    var table = document.getElementById("table1");
    var tr = table.insertRow();
    var td = tr.insertCell();
    td.innerHTML= "a";

    td = tr.insertCell();
    td.innerHTML= "b";
    
    td = tr.insertCell();
    td.innerHTML= "c";
    
    td = tr.insertCell();
    td.innerHTML= "d";
    
    td = tr.insertCell();
    td.innerHTML= "e";
 }
</script>
<body>
    <table id="table1" border="1" cellpadding="0" cellspacing="0" width="100%">
        <tr id="row1">
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
    </table>
    <input type="button" onClick="addNewRow()" value="Add New"/>
</body>

此示例用于在表中动态插入新行和单元格。 但它的行为在所有浏览器中都不同。

  1. Internet Explorer = 在最后添加行,新添加的单元格从第一个开始。

  2. Chrome/Safari = 它在第一个中添加新行,并且新添加的单元格从末尾开始。

  3. Mozilla Firefox = 无法正常工作。

    我希望在最后一个新添加的行,并且新添加的单元格从第一个开始,就像所有浏览器中的 (Internet Explorer) 一样。

如果您对相同行为有任何解决方案,请告诉我。

<script type="text/javascript" language="javascript">
 function addNewRow()
 {
    var table = document.getElementById("table1");
    var tr = table.insertRow();
    var td = tr.insertCell();
    td.innerHTML= "a";

    td = tr.insertCell();
    td.innerHTML= "b";
    
    td = tr.insertCell();
    td.innerHTML= "c";
    
    td = tr.insertCell();
    td.innerHTML= "d";
    
    td = tr.insertCell();
    td.innerHTML= "e";
 }
</script>
<body>
    <table id="table1" border="1" cellpadding="0" cellspacing="0" width="100%">
        <tr id="row1">
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
    </table>
    <input type="button" onClick="addNewRow()" value="Add New"/>
</body>

This example is for dynamically insert new row and cells in the table.
But its behavior is different in all browsers.

  1. Internet Explorer = It add row in the last and new added cells are starts from first.

  2. Chrome/Safari = It add new row in the first and new added cells are starts from end.

  3. Mozilla Firefox = It is not working.

    I want new added row in the last and new added cells starts from first like (Internet Explorer) in all browsers.

If you have any solution for same behavior please tell me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如若梦似彩虹 2024-09-13 00:22:28

尝试使用:

var tr = table.insertRow(-1);

就像这里所说的:

https://developer.mozilla.org/en /DOM/table.insertRow

try to use:

var tr = table.insertRow(-1);

like its said here:

https://developer.mozilla.org/en/DOM/table.insertRow

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文