Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

javascript - Why is document.getElementById('tableId').innerHTML not working in 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 Code:

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

Java Script code:

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since the problem is in IE8, see MSDN:

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.

(emphasis mine)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...