javascript添加行 [英] javascript add row

查看:71
本文介绍了javascript添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我想在表中添加一行,但是我想在最后一行之前添加该行.
我该怎么办?

请帮助我.

Hi everyone!

I want to add a row in my table but I want to add that row before the last row.
how will I do that?

Please help me.

推荐答案

您可以使用DOM遍历节点,直到找到最后一行,然后在其中插入一行.查找表的方式取决于触发事件的原因,是否需要查找表元素b/c,然后还可以存储最后一行的ID并直接找到它.
You can use the DOM to iterate over nodes until you find the last row, then insert a row there. How you find the table depends on what triggers the event, do you need to look up the table element, b/c then you may as well store the id of the last row and find it directly.


ahm ...,实际上我已经使用了DOM,它附加在最后一行,但是我希望它附加在最后一行之前.

我该怎么办?

这是我的代码:

ahm...,actually i have use the DOM, it appends in the last row but i want it to append is before the last row.

How should i do that?

here is my code:

my JavaScript code...

function TbladdRow(){
    var table = document.getElementById('display');
    var tr = document.createElement('TR');
    var th1 = document.createElement('TH');
        th1.innerHTML = "andrew";
    var th2 = document.createElement('TH');
        th2.innerHTML = "andrew";
    var th3 = document.createElement('TH');
        th3.innerHTML = "andrew";
    var th4 = document.createElement('TH');
        th4.innerHTML = "andrew";
    var th5 = document.createElement('TH');
        th5.innerHTML = "<input type='button' name='addRow' value='ADD ROW' onclick='TbladdRow();'>";
    tr.appendChild(th1);
    tr.appendChild(th2);
    tr.appendChild(th3);
    tr.appendChild(th4);
    tr.appendChild(th5);
    table.appendChild(tr);
}

my html code(table)...

    <table style="width: 500px;" border="1px" id="display" align="center">
        <thead>
            <tr>
                <th>ROOMS</th><th>SERVICES</th><th>MEDICINE</th><th>TREATMENT</th><th>METHOD</th>
            </tr>
        </thead>
        <tr>
            <th>
                <select name="room" id="Room">
                    <option selected="selected">--ROOM NO--</option>
                </select>
            </th>
            <th>
                <select name="services" id="Services">
                    <option selected="selected">--SERVICES NAME--</option>
                </select>
            </th>
            <th>
                <select name="medicines" id="Medicines">
                    <option selected="selected">--MEDICINES--</option>
                </select>
            </th>
            <th>
                <select name="treatment" id="Treatment">
                    <option selected="selected">--TREATMENT--</option>
                </select>
            </th>
            <th><input type="button" name="addRow" value="ADD ROW" onclick="TbladdRow();"></th>
        </tr>
        /* .....I WANT IT HERE TO APPEAR.....*/
        <tr>
            <th align="center" colspan="5"><input type="submit" name="admit" value="submit" />&nbsp;&nbsp;&nbsp;<input type="button" name="cancel" value="CANCEL" onclick="window.close();"></th>
        </tr>
    </table>


这篇关于javascript添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆