如何在单元格中添加colspan和文本?
I'm getting an error when I insert a row and a cell, add a text (innerHTML, textContent) and a colspan. The error reads:
插入行和单元格时添加文本(innerHTML,textContent)和colspan时出错。错误如下:
Safari:
"TypeError: Attempted to assign to readonly property."
“TypeError:尝试分配给只读属性。”
Chrome:
"Uncaught TypeError: Cannot assign to read only property"
“未捕获的TypeError:无法分配给只读属性”
If I remove the text, the colspan will work and any other cells will show; if I remove the colspan, the text will show. If I try to keep both, apart from getting the error, any other cells that exist will disappear, and the colspan won't work.
如果我删除文本,colspan将工作,任何其他单元格将显示;如果我删除了colspan,文本将显示。如果我试图保持两者,除了得到错误,任何其他存在的单元格将消失,并且colspan将无法工作。
Example html:
<table id ="my_table">
<thead>
<tr>
<th>text</th>
<th>text</th>
<th>text</th>
<th>text</th>
<th>text</th>
</tr>
Example 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";
...or
var total = my_row.insertCell(0).innerHTML = "my colspan text";
...then
total.colSpan = "4";
}
When I searched for this problem, I read that this exists in iOS8 and that it occurs when use in strict mode; however, if I remove "use strict", the error message will disappear, but the problem will persist. Some say it's a webkit bug, but the same thing happens in Firefox, minus the error message.
当我搜索这个问题时,我读到它存在于iOS8中并且在严格模式下使用时会出现;但是,如果我删除“use strict”,错误消息将消失,但问题将持续存在。有人说这是一个webkit错误,但同样的事情发生在Firefox中,减去错误信息。
Possibly related link: TypeError: Attempted to assign to readonly property. on iOS8 Safari
可能相关的链接:TypeError:尝试分配给readonly属性。在iOS8 Safari上
Am I doing something wrong or is there a workaround?
我做错了什么或有解决方法吗?
Addendum: a sub-optimization is to add a second cell that has no text inserted, and give it a colspan.
附录:子优化是添加没有插入文本的第二个单元格,并给它一个colspan。
1 个解决方案
#1
my_row.insertCell(0).textContent = "my colspan text"
doesn't return the cell, it returns the string assigned to textContent
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>
更多相关文章
- 从打印页面中删除网址并打印文本
- 2_python连接MariaDB错误
- 获取错误“ValueError:int()的无效文字,基数为10:'3128;'在运行Tensor
- GIS / Gdal / OSGeos在Windows上的django中导入错误
- Python第十天 print >> f,和fd.write()的区别 stdout的buffer 标
- Hive中使用Python实现Transform时遇到Broken pipe错误排查
- 'module'对象没有属性'views' django错误
- Pandas 文本数据方法 findall( )
- 当我运行createsuperuser获取HTTP 401错误