javascript html标签不添加内容

时间:2015-10-07 20:47:11

标签: javascript jquery html

我正在尝试将内容作为表格附加到div

<div id="viewcontent"></div>
<script>
$('#viewcontent').html = $('#viewcontent').html + "<table bgcolor='#CCCCFF' bordercolor='#000000' align='center' width='600' border='1'>";

for (var i = 1, len = msg.length; i < len; i=i+6) {
    if (msg[i+1] == 1) { //misc
        var rtype = "Miscellaneous"
        var rcolor = "#CCCCFF"
    } else if (msg[i+1] == 2) { //verbal
        var rtype = "Verbal"
        var rcolor = "#FFF000"
    } else if (msg[i+1] == 3) { //written
        var rtype = "Written"
        var rcolor = "#FFAA00"
    } else if (msg[i+1] == 4) { //Critical
        var rtype = "Critical"
        var rcolor = "#FF0000"
    } else if (msg[i+1] == 5) { //Positive
        var rtype = "Positive"
        var rcolor = "#55FF22"
    } else if (msg[i+1] == 6) { //Performance Review
        var rtype = "Performance Review"
        var rcolor = "#C8C8C8"
    }

    $('#viewcontent').html = $('#viewcontent').html + "<tr><td colspan='1' align='center' border='1' width='5%'><input type='radio' name='RID' value='" + msg[i+5] + "'></td><td class='file' colspan='1' align='center' border='1' width='20%' bgcolor='" + rcolor + "'><a href='upload_files/" + emp + "/" + msg[i+2] + "'>" + rtype + "</a></td><td colspan='1' align='center' border='1' width='15%'>" + msg[i] + "</td><td colspan='1' align='center' border='1' width='20%'>" + msg[i+4] + "</td><td colspan='1' align='center' border='1' width='40%'>" + msg[i+3] + "</td></tr>";

}
$('#viewcontent').html = $('#viewcontent').html +   "</table>";
</script>

这导致空Div - 使用innerhtml时发送的内容我收到的内容没有任何标签

如何在不删除和维护我的HTML

的情况下附加到此内容

2 个答案:

答案 0 :(得分:3)

jQuery .html属性是函数

$("something").html("some content");

同样,当你想获取内容时:

var theContents = $("something").html();

答案 1 :(得分:1)

尝试以下

<div id="viewcontent"></div>



<script>
$('#viewcontent').html('');
             $('#viewcontent').append("<table bgcolor='#CCCCFF' bordercolor='#000000' align='center' width='600' border='1'>");

                for (var i = 1, len = msg.length; i < len; i=i+6) {
                    if (msg[i+1] == 1) { //misc
                        var rtype = "Miscellaneous"
                        var rcolor = "#CCCCFF"
                    } else if (msg[i+1] == 2) { //verbal
                        var rtype = "Verbal"
                        var rcolor = "#FFF000"
                    } else if (msg[i+1] == 3) { //written
                        var rtype = "Written"
                        var rcolor = "#FFAA00"
                    } else if (msg[i+1] == 4) { //Critical
                        var rtype = "Critical"
                        var rcolor = "#FF0000"
                    } else if (msg[i+1] == 5) { //Positive
                        var rtype = "Positive"
                        var rcolor = "#55FF22"
                    } else if (msg[i+1] == 6) { //Performance Review
                        var rtype = "Performance Review"
                        var rcolor = "#C8C8C8"
                    }

    $('#viewcontent').append("<tr><td colspan='1' align='center' border='1' width='5%'><input type='radio' name='RID' value='" + msg[i+5] + "'></td><td class='file' colspan='1' align='center' border='1' width='20%' bgcolor='" + rcolor + "'><a href='upload_files/" + emp + "/" + msg[i+2] + "'>" + rtype + "</a></td><td colspan='1' align='center' border='1' width='15%'>" + msg[i] + "</td><td colspan='1' align='center' border='1' width='20%'>" + msg[i+4] + "</td><td colspan='1' align='center' border='1' width='40%'>" + msg[i+3] + "</td></tr>");

}
$('#viewcontent').append("</table>");
</script>

希望,这会有所帮助!