如何使用JavaScript将数据显示为HTML

使用javascript将数据显示为HTML时出现问题.我创建的代码显示最新数据,而不显示整个数据.我在开发中使用phonegap.这是代码

 var oldHtml = document.getElementById("favorite-table-id").innerHTML;

 for(var i=0; i<courseIdResult.length;i++) {
    //var idResult = courseIdResult[i];
    db.transaction(
            function(tx) {
                var query='SELECT Title,Institution_Id,FullTime,Entryscore,Prerequisites FROM Course WHERE Id='+courseIdResult[i];
                tx.executesql(query,[],function(tx,resultSet) {
                    console.log("Test 0");
                    var row = resultSet.rows.item(0);
                    var newHTML = "<tr> <td> <table> <tr> <td width=1% style='white-space:Nowrap;font-weight:bold' align='left'>" +row['Title']+"-"+row['Institution_Id'] +
                      "</td> <td align='right' style='vertical-align:middle;' rowspan=2> <a href='#'><span class='glyphicon glyphicon-star'></span></a> </td></tr> <tr> <td width=1% style='white-space:Nowrap'>" +
                      "Years:"+row['FullTime']+ "  ATAR:"+row['Entryscore']+ "  Prereq:"+row['Prerequisites']+ 
                      "</td> </tr> </table> </td> </tr>";
                    document.getElementById("favorite-table-id").innerHTML= oldHtml + newHTML;
                },errorCB);
            },errorCB
        );
}

变量courseIdResult是我的查询的ID,我正在使用循环获取数据.但是,每次循环进行时,document.getElementById(“ favorite-table-id”).innerHTML的值始终会被覆盖.请帮我解决这个问题.

谢谢

解决方法:

var q = "";
for (var i = 0; i < courseIdResult.length; i++) {
    q += (q == "" ? "" : ", ") + "?";
}
var query = 'SELECT Title,Institution_Id,FullTime,Entryscore,Prerequisites FROM Course WHERE Id IN (' + q + ')';
var db = CreateDB();
var row = '';
db.transaction(populateDB, errorDB, successDB);
function populateDB(tx) {
    tx.executesql(query, courseIdResult, function (tx, results) {
        var len = results.rows.length;
        var arrSectionTableName = [];
        if (len > 0) {
            $("#favorite-table-id").empty();
            var newHTML = "";
            for (var i = 0; i < len; i++) {
                 newHTML += "<tr> <td> <table> <tr> <td width=1% style='white-space:Nowrap;font-weight:bold' align='left'>" +row['Title']+"-"+row['Institution_Id'] +
                  "</td> <td align='right' style='vertical-align:middle;' rowspan=2> <a href='#'><span class='glyphicon glyphicon-star'></span></a> </td></tr> <tr> <td width=1% style='white-space:Nowrap'>" +
                  "Years:"+row['FullTime']+ "  ATAR:"+row['Entryscore']+ "  Prereq:"+row['Prerequisites']+ 
                  "</td> </tr> </table> </td> </tr>";
            }
            $('#"favorite-table-id"').val(newHTML);
           //Refresh your control,eg if it is a listview
            $('#favorite-table-id').listview('refresh');
        }
    });
}
function errorDB(err) {
    alert("Error processing sql " + err.message);
}
function successDB() {

}

相关文章

公司前端界面用的是vue,我要嵌入到Android中生成App第一步:...
Q:我用cordova开发项目,想在app内跳转外部链接,安装了cord...
我正在使用https://github.com/arnesson/cordova-plugin-fir...
一、Cordova的基础点在混合式应用中,我们通过现有的Cordova...
cordova自定义插件注意:存放自定义cordova插件目录不能有空...
一、问题VueAPP中有一个文件下载功能,用了各种方法来实现下...