我正在尝试为某些表格单元格提供 FontAwesome 字形的能力。这是通过使用适当的 CSS 类来实现的,例如<i class="fa fa-check-square-o fa-2x foobar-stat-green"></i>
这是我迄今为止尝试过的 JS :
function addRow(tableID,rowData) {
if (Array.isArray(rowData) && rowData.length>0) {
var myTab=document.getElementById(tableID).getElementsByTagName('tbody')[0];
var newRow = myTab.insertRow();
rowData.forEach(function (item) {
console.log(item);
var newCell = newRow.insertCell();
// THIS : createTextNode + appendChild = WORKS OK (no "err") !
//var newText = document.createTextNode(item);
//newCell.appendChild(newText);
// SWITCHING TO THIS = WORKS OK (no "err" in console)
//newCell.innerHTML='';
// BUT THIS YIELDS "err" in console
//newCell.innerHTML(item);
});
}
}
此函数的示例输入为:
var foobar = ['a','b','c','test+a@example.com','10/10/10 10:10','<i class="fa fa-check-square-o fa-2x foobar-status-green"></i>',false];
最佳答案
innerHTML
不是一个函数,它是一个属性,你不用括号,直接写:
newCell.innerHTML = item;
无需先写:
newCell.innerHTML = ''; // not needed
关于cell.innerHTML() 的 JavaScript 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46059627/