为什么 document.getElementById('tableId').innerHTML 在 IE8 中不起作用? [英] Why is document.getElementById('tableId').innerHTML not working in IE8?

查看:22
本文介绍了为什么 document.getElementById('tableId').innerHTML 在 IE8 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面中使用 Java Script 修改了 document.getElementById('').innerHTML.它在 Firefox 中运行良好,但不适用于 IE8.请参阅下文了解更多详情:

I modify document.getElementById('').innerHTML with Java Script in a page. It's working fine in Firefox, but not IE8. Please see below for more details:

HTML 代码:

<table>
  <tr id="abc">
     <td id="ccc" style="color:red;">ccc</td>
  </tr>
</table>

Java 脚本代码:

  document.getElementById('abc').innerHTML = '<td id="bbc" style="color:yellow;">abc</td>'

当我在 Firefox 中运行 JS 代码时,它会将显示字从ccc"更改为abc",但它在 IE8 中不起作用,有人知道为什么吗?有什么办法可以让我在 IE8 中也能正常工作吗?

When I run the JS code in Firefox, it will change the display word from 'ccc' to 'abc', but it's just not working in IE8, does anyone know why? Is there any way I can make this work in IE8 as well?

推荐答案

由于问题出在 IE8,见 MSDN:

Since the problem is in IE8, see MSDN:

该属性对于除以下对象外的所有对象都是可读/可写的:COL、COLGROUP、FRAMESET、HEAD、HTML、STYLE、TABLE、TBODY、TFOOT、THEAD、TITLE、TR.

The property is read/write for all objects except the following, for which it is read-only: COL, COLGROUP, FRAMESET, HEAD, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, TR.

(强调我的)

Colin 的解决方法(设置 <td 上的 code>innerText 而不是 tr 上的 innerHTML )在您的情况下是一个很好的选择.如果您的需求变得更加复杂,您将不得不求助于 The Table对象模型.

Colin's work-around (setting innerText on the td instead of innerHTML on the tr) is a good one in your case. If your needs become more complex, you'll have to resort to The Table Object Model.

这篇关于为什么 document.getElementById('tableId').innerHTML 在 IE8 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆