JavaScript从动态创建的表中自定义文本

时间:2017-12-08 07:07:29

标签: javascript html

尝试使用JavaScript动态地将表格添加到HTML时,我遇到了一些问题。

我有一个数组,我的示例数据是[0,0,5,2,7,5,4,5,0]

我的HTML:

<div id="ftw" style="min-width:80px; overflow:auto; overflow-x:hidden; overflow-y:hidden; display:none;">
    <table id="ft" class="table" style="font-size:13.5px">

    </table>
</div>

我的JavaScript:

document.getElementById('ftw').style.display = 'block';
        var table = document.getElementById("ft");

        // helper function        
        function addCell(tr, text) {
            var td = tr.insertCell();
            td.textContent = text;
            return td;
        }

我想做item == 0之类的事情,然后我将字体设置为红色。如果item > 0,我想加粗item

任何想法我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

这里是JsFiddle ..

我将td.textContent更改为td.innerHTML并添加row.style.color="red"; addCell(row, 'Every <b>' + item + '</b> day(s)');

 function addCell(tr, text) {
            var td = tr.insertCell();
            td.innerHTML = text;
            return td;
        }



dataset.forEach(function (item) {
            var row = table.insertRow();
            if(item == 0){
            row.style.color="red";
                addCell(row, 'No record');
            }else{  
                addCell(row, 'Every <b>' + item + '</b> day(s)');

            }
        });
相关问题