繁体   English   中英

jQuery将表追加到div

[英]jquery to append a table to div

我有一个div id 。我必须向div id追加一个带有值的表。

<div id="testResult" style="padding-left: 120px; "></div>

正在执行以下步骤,但无法正常工作。

$.ajax({
    url: '/getReports',
    cache: false
}).done(function (html) {
    if (html != "") {
        $('#testResult').show();
        var htm = "";

        var string1 = '<table border="1"><tr><td>No</td><td>Testcase</td> <td>OS</td> <td>Browser</td>  <td>Result</td></tr></table>';
        $.each(html, function (i, data) {

            string1 + = '<tr><td rowspan="3">1</td><td rowspan="3">' + data.status.test + '</td><td rowspan="3"><!--OS--></td><td>' + data.status.bwser + '</td> <td> ' + data.status.report + ' </td></tr>';
        });
        $('#testResult ').append(string1);
        $("#testResult").html("<br/><br/>");
        $("#testResult").html("<p>" + htm + "</p>");
    }

}).fail(function () {
    $("#testResult").html("Failed to run the test");
    $('#edit ').removeAttr("disabled");
});

$("#testResult").html("<br/><br/>")$("#testResult").html("<p>" + htm + "</p>")将覆盖内容您的“ testResult” DIV。 所以,取代这个

$('#testResult ').append(string1);
      $("#testResult").html("<br/><br/>");
      $("#testResult").html("<p>" + htm + "</p>");   
}

$('#testResult ').append(string1);
      $("#testResult").append("<br/><br/>");
      $("#testResult").append("<p>" + htm + "</p>");   
}

.append() :将额外的字符串添加或附加到DIV的现有内容中,其中

.html() :将删除或覆盖较新版本的DIV的现有内容。

这是.append().html()之间的主要区别。

编辑这部分

   $("#testResult").append("<br/><br/>");
  Remove this part-->
  $("#testResult").html("<p>" + htm + "</p>");   

htm是空字符串,因为您永远不会将任何字符串连接到它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM