1

大家好,我正在绘制动态表,我想为表添加表头如何将表头添加到动态表这里是我的表代码:

$.get('http://myDomain.com/RIA/topIndiaDetails.asp', function(data)
{    
    console.log("####"+data);

    var up = new Image();
    var down = new Image();

    up.src = "../../jquery.mobile/images/up.png";
    down.src = "../../jquery.mobile/images/down.png"

    substr = data.split('$');

    //alert(substr.length);
    //var theader = '<table border="1">\n';
    //<table id="auditOverview" border="1">
    var theader = '<table border="1" id=\"tableId\">\n';
    var tbody = '';

    for (var out = 1;out<substr.length-1;out++)
    {
        //alert(substr[out]);
        tbody += '<tr>';
        var pra = substr[out].split('|^');
        //alert('pra.length is: '+pra.length);

        for (var i=0;i<pra.length-1;i++)
        {
            tbody += '<td>';

            if (pra[i]=="Red")
            {
                pra[i].append("<img id='theImg' src='../../jquery.mobile/images/down.png'/>");
            }
            else if (pra[i]=="Green")
            {
                pra[i].append("<img id='theImg' src='../../jquery.mobile/images/up.png'/>");
            }
            tbody += pra[i];    
            tbody += '</td>'  
        }
        tbody += '</tr>\n';
    }       
    var tfooter = '</table>';
    document.getElementById('wrapper').innerHTML = theader + tbody + tfooter;       

});

任何帮助将不胜感激。提前致谢。

4

3 回答 3

2

您可以构建标题:

var header = "<th>...</th>";

然后:

$("table").append(header);
于 2012-06-16T10:54:35.883 回答
1

如果我能正确理解你的观点

演示:http: //jsfiddle.net/75zFX/8/

var colheader = '<tr><th>Column1</th><th>Column2</th> .....</tr>\n';
var tbody = colheader ;// and replace with var tbody = '';
于 2012-06-16T10:56:31.353 回答
1

如果您想手动将标题添加到表中,请使用此代码。

JSP

<th id="example"></th>

JavaScript

$("#example").html("Header Value"); 
于 2015-02-20T16:23:42.207 回答