jQuery 我可以在这个 javascript 函数中使用 append/appendTo [英] jQuery can I use append/appendTo in this javascript function

查看:23
本文介绍了jQuery 我可以在这个 javascript 函数中使用 append/appendTo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个函数,该函数成功地将行/单元格附加到表格中,并从数组中填充它们.问题是它们被附加到表格的顶部.

I have created a function that successfully appends a table with rows/cells and populates them from an array. The problem is that they are being appended to the top of the table.

这样做是因为 var row = tbody.insertRow(r);...我认为/希望.

It is doing this because the line var row = tbody.insertRow(r);...I think/hope.

下面链接中要查看的函数是appendTable(id)

The function to look at in the link below is appendTable(id)

这是附加到顶部的工作示例:http://jsfiddle.net/JEAkX/4/

Here is the working example that appends to the top: http://jsfiddle.net/JEAkX/4/

我在想这样的事情会起作用:var row = insertRow(r).appendTo('tbody>tr:last'); 但是它只是破坏了功能.

I was thinking something like this would work: var row = insertRow(r).appendTo('tbody>tr:last'); however it just broke the function.

我还在同一行上尝试了 .append().appendTo() 的许多不同组合,但没有成功.作为最后的努力,我尝试了cell.innerHTML = "<input type='text' size='1' value='" + subset[i++] + "'/>"但我相信那个时候位置已经确定了.

I also tried MANY different combinations of .append() and .appendTo() on that same line with no success. As a last ditch effort I tried variations on the linecell.innerHTML = "<input type='text' size='1' value='" + subset[i++] + "' />" but I believe by that time the location is already decided.

这是从示例中提取的函数:

Here is the function extracted from the example:

function appendTable(id)
{
    var tbody = document.getElementById(id).getElementsByTagName("tbody")[0];
    var i = 0;
    for (var r = 0; r < 2; r++) {
        var row = tbody.insertRow(r);
        for (var c = 0; c < 4; c++) {
            var cell = row.insertCell(c);
            cell.innerHTML = "<input type='text' size='1' value='" + subset[i++] + "' />"
        }
    }
}

一方面,我没有尝试过(失败)insertAfter,但读到我将无法在页面加载时不存在的内容上使用它.

On a side not I tried (unsuccessfully) insertAfter at one point but read that I would not be able to use it on something that didn't exist on page load.

推荐答案

insertRow 在指定的索引处添加一行.

insertRow adds a row at the specified index.

当该参数(代码中的 r)为零时,它会将行添加到表格的顶部.要将它们添加到末尾,请使用表格的当前长度作为起点:

When that parameter (r in your code) is zero, it adds the row to the top of the table. To add them to the end instead, use the current length of the table as your starting point:

function appendTable(id)
{
    var tbody = document.getElementById(id).getElementsByTagName("tbody")[0];
    var length = tbody.rows.length;
    for (var r = length; r < length + 1; r++) {
        var row = tbody.insertRow(r);
        // etc.
    }
}

这篇关于jQuery 我可以在这个 javascript 函数中使用 append/appendTo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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