jQuery的我可以用这个javascript函数追加/ appendTo [英] jQuery can I use append/appendTo in this javascript function

查看:247
本文介绍了jQuery的我可以用这个javascript函数追加/ 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行= 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行=的insertRow(R).appendTo('TBODY> TR:去年'); 但它只是打破了功能

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 =变化<输入类型=文本大小='1'值='+子集[我++] +'/> ,但我相信那个时候的位置已经确定。

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.

在这个参数(研究在code)是零,它增加了该行表的顶部。
将它们添加到结束而应使用表的当前长度作为起点:

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函数追加/ appendTo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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