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>

更多相关文章

  1. 从打印页面中删除网址并打印文本
  2. 2_python连接MariaDB错误
  3. 获取错误“ValueError:int()的无效文字,基数为10:'3128;'在运行Tensor
  4. GIS / Gdal / OSGeos在Windows上的django中导入错误
  5. Python第十天 print >> f,和fd.write()的区别 stdout的buffer 标
  6. Hive中使用Python实现Transform时遇到Broken pipe错误排查
  7. 'module'对象没有属性'views' django错误
  8. Pandas 文本数据方法 findall( )
  9. 当我运行createsuperuser获取HTTP 401错误

随机推荐

  1. golang的优势在哪里
  2. golang可以写web吗?
  3. golang结构体怎么判断是否为空
  4. golang判断map是否存在key
  5. 2021年放弃Python选择Go的原因
  6. golang接口可以嵌套吗
  7. 运行golang程序提示进程无法启动
  8. golang mysql操作介绍
  9. golang反向代理无法访问
  10. golang项目如何部署到linux服务器