插入行和单元格,添加文本(innerHTML,textContent)和colspan时出现错误。该错误显示为:
苹果浏览器:
“ TypeError:试图分配给只读属性。”
铬合金:
“未捕获的TypeError:无法分配为只读属性”
如果我删除文本,则colspan将起作用,并且其他所有单元格都将显示;如果删除colspan,则将显示文本。如果我尝试同时保留两者,则除了得到错误之外,其他所有存在的单元都将消失,并且colspan将无法工作。
范例html:
<table id ="my_table">
<thead>
<tr>
<th>text</th>
<th>text</th>
<th>text</th>
<th>text</th>
<th>text</th>
</tr>
JS示例:
function test3() {
//get the table id;
var table = document.getElementById('my_table');
//create row, cell
var my_row = table.insertRow(-1);
var total = my_row.insertCell(0).textContent = "my colspan text";
...或者
var total = my_row.insertCell(0).innerHTML = "my colspan text";
...然后
total.colSpan = "4";
}
当我搜索此问题时,我读到它存在于iOS8中,并且在严格模式下使用时会发生。但是,如果我删除“ use strict”,则错误消息将消失,但问题仍然存在。有人说这是一个Webkit错误,但是减去错误消息后,在Firefox中也发生了同样的事情。
可能相关的链接:TypeError:尝试分配给只读属性。在iOS8 Safari上
我是在做错什么还是有解决方法?
附录:次优化是添加第二个未插入文本的单元格,并为其赋予colspan。
my_row.insertCell(0).textContent = "my colspan text"
不返回单元格,它返回分配给的字符串 textContent
function test3() {
//get the table id;
var table = document.getElementById('my_table');
//create row
var my_row = table.insertRow(-1);
//create cell
var total = my_row.insertCell(0);
//set properties
total.textContent = "my colspan text";
total.colSpan = 4;
}
window.addEventListener('load', test3, false);
<table id="my_table" border="1">
<thead>
<tr>
<th>text</th>
<th>text</th>
<th>text</th>
<th>text</th>
<th>text</th>
</tr>
</thead>
</table>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句