Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript根据行位置将表一分为二_Javascript - Fatal编程技术网

使用javascript根据行位置将表一分为二

使用javascript根据行位置将表一分为二,javascript,Javascript,我想根据行位置将一个表拆分为两个表。我有办法找到表的行位置。现在,我的要求是仅使用javascript将表拆分为两个表。希望它有帮助: function splitTableIntoTwoTables(table, idx) { var rows = table.getElementsByTagName("tr"), newtable = table.cloneNode(false), tbody = document.createElement("tb

我想根据行位置将一个表拆分为两个表。我有办法找到表的行位置。现在,我的要求是仅使用javascript将表拆分为两个表。

希望它有帮助:

function splitTableIntoTwoTables(table, idx) {

    var rows = table.getElementsByTagName("tr"),
        newtable = table.cloneNode(false),
        tbody = document.createElement("tbody");

    if (table.hasAttribute("id")) {
        newtable.setAttribute("id", "splitted_"+table.getAttribute("id"));
    }

    for (var i = rows.length - 1; i > idx; i--) {
        if (tbody.childNodes) {
            tbody.insertBefore(rows[i].cloneNode(true), tbody.lastChild);
        } else {
            tbody.appendChild(rows[i].cloneNode(true));
        }
        table.deleteRow(i);
    }

    newtable.appendChild(tbody);
    var p = document.createElement("p");
    p.appendChild(document.createTextNode("=============>"));
    if (table.nextSibling) {
        table.parentNode.insertBefore(p, table.nextSibling);
    } else {
        table.parentNode.appendChild(p);
    }
    if (p.nextSibling) {
        table.parentNode.insertBefore(newtable, p.nextSibling);
    } else {
        table.parentNode.appendChild(newtable);
    }
}

请试试我的变体。适用于带或不带tbodies的表。它需要一个表和一个行索引,然后再进行分割

function split(table, index){
    var temp, clone = table.cloneNode(false),
        trs = table.getElementsByTagName('tr'),
        tbodies = table.getElementsByTagName('tbody');
    if (++index>=trs.length) return;
    if (clone.id) clone.id+='-clone';
    while (index>0) {
        if(tbodies[0] && (temp = tbodies[0].getElementsByTagName('tr').length) <= index) {
            index -= temp;
            clone.appendChild(tbodies[0]);
        } else {
            temp|0 && (temp = tbodies[0] ? clone.appendChild(tbodies[0].cloneNode(false)) : clone);
            temp.appendChild(trs[0]);
            index--;
        }
    }
    table.parentNode.insertBefore(clone, table);
};
函数拆分(表、索引){
变量温度,克隆=表.克隆节点(假),
trs=table.getElementsByTagName('tr'),
tbodies=table.getElementsByTagName('tbody');
如果(++索引>=trs.length)返回;
如果(clone.id)clone.id+='-clone';
而(索引>0){

如果(tbodies[0]&&(temp=tbodies[0].getElementsByTagName('tr').length)您是指HTML表吗?我们需要更多:显示您已经拥有的代码以及您尝试过的内容。请毫不犹豫地使用。使用getElementsByTagName()非常好,因为它确实比querySelectorAll()快得多!为什么要将克隆放在原始的上面?I.m.h.o.像“prefix”+字符串这样的前置字符串总是比像字符串+=“postfix”这样的追加更快,占用的内存更少(请参阅Java中的StringBuffer)。我也认为拥有它总是好的。顺便说一句,你的变体导致克隆在MSIE 10中至少有两个s;)(我已经用带有和标题的表进行了测试)。所以,现在我们都有机会编写一个非常好的单拆分函数;)我将克隆放在上面,因为它传输第一行比传输最后一行更快。
temp | 0&&(temp=
-足够酷了,哈?测试temp是否为element是的,这是一种有趣的方法。但是在你的函数在MSIE中生成有效的HTML之前,我不能给你+到karma;)