将带有javascript函数的span标记添加到html表中

Add span tag with javascript function to html table

本文关键字:添加 html 表中 span javascript 函数      更新时间:2023-09-26

我在javascript中有一个函数可以更改html表中某些单元格的背景色

<script language="javascript">
function GetAllValues() {
    var tbl = document.getElementById("monitor");
        for (var i = 0; i < tbl.rows.length; i++){
                if (parseInt(tbl.rows[i].cells[6].innerHTML) == 1){
                         tbl.rows[i].cells[5].style.backgroundColor = "green";
                }else if (parseInt(tbl.rows[i].cells[6].innerHTML) == 2){
                        tbl.rows[i].cells[5].style.backgroundColor = "yellow";
                }else if (parseInt(tbl.rows[i].cells[6].innerHTML) == 3){
                        tbl.rows[i].cells[5].style.backgroundColor = "red";
                }else if (parseInt(tbl.rows[i].cells[6].innerHTML) == 5){
                        tbl.rows[i].cells[5].style.backgroundColor = "gray";
                }else{
                        tbl.rows[i].cells[5].style.backgroundColor = "white";
                        }
</script>

但我需要在单元格的值上添加一个"span"标签,如下所示:

<span class="label label-success">table cell value</span>

我不知道如何将这个标签添加到单元格中。

你能给我一些片段或链接吗?在那里我可以提出一些想法。

提前感谢

通过连接span标记将innerHTML设置为当前innerHTML值。

tbl.rows[i].cells[6].innerHTML = '<span>' + tbl.rows[i].cells[6].innerHTML + '</span>'