javascript - 使用 innerHTML 的 javascript Bootstrap 表

标签 javascript html twitter-bootstrap

我正在尝试使用以下代码让表格正确显示。问题是 </tbody></table>在 for 循环将数据输入表之前应用标签。表头显示正常,数据也显示正常,只是不在表中。我究竟做错了什么?我尝试使用 in if 语句将最后一行移动到循环中,但同样发生了。

    $.getJSON("adriver.php?type=get", function(data, status){
    document.getElementById("aRightContent").innerHTML =
            "<table class='table table-hover'>" +
            "<thead>" +
            "<tr>" +
                "<th><i class='fa fa-check-square'></iclass></th>" +
                "<th>ID</th>" +
                "<th>Firstname</th>" +
                "<th>Lastname</th>" +
                "<th>UserName</th>" +
            "</tr>" +
            "</thead>" +
            "<tbody>";

    for(var i = 0; i < data.length; i++){
        document.getElementById("aRightContent").innerHTML +=
                "<tr>" +
                    "<td><input type='radio' name='selectedDriver' value='" + data[i].dID + "'></td>" +
                    "<td>" + data[i].dID + "</td>" +
                    "<td>" + data[i].dFirstName + "</td>" +
                    "<td>" + data[i].dLastName + "</td>" +
                    "<td>" + data[i].dUsername + "</td>" +
                "</tr>";
    }
    document.getElementById("aRightContent").innerHTML += "</tbody></table>";
});

感谢您的帮助。

最佳答案

您不能添加部分 <table></table>使用 innerHTML ,因为浏览器倾向于为您填写。我建议的最佳方法是构建整个 HTML,然后附加。

所以你应该有这样的东西:

finalHTML = "<table class='table table-hover'>" +
        "<thead>" +
        "<tr>" +
            "<th><i class='fa fa-check-square'></iclass></th>" +
            "<th>ID</th>" +
            "<th>Firstname</th>" +
            "<th>Lastname</th>" +
            "<th>UserName</th>" +
        "</tr>"
        "</thead>" +
        "<tbody>";
finalHTML += "..."  // Other code.

此外,由于您使用的是 jQuery,因此请使用相同的组件。而不是 document.getElementById("aRightContent").innerHTML ,使用:

$("#aRightContent").html()

关于javascript - 使用 innerHTML 的 javascript Bootstrap 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33066453/

相关文章:

javascript - 从 Knockout 可观察数组中删除表中的选定行

javascript - Ace 编辑器模式不起作用

html - 外部 CSS 文件不适用于 Bootstrap CSS

javascript parseFloat '500,000' 在我需要 500000 时返回 500

javascript - 如何在按钮上单击 JqGrid 文本框值

javascript - 在使用 skrollr.js 和 ID 为 "skrollr-body"的多个 div 时,Scroll 在 android 中不起作用

jquery - 如何创建具有一定边距的全屏背景视频?

html - 如何使两列布局具有等高列和一个可滚动列?

javascript - 推特 Bootstrap ".input-append"不适用于弹出窗口

javascript - 如何集成 Bootstrap 日期选择器