Why is document.getElementById('tableId')[removed] not working in IE8?

前端 未结 8 2048
迷失自我
迷失自我 2020-12-01 14:17

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:

相关标签:
8条回答
  • 2020-12-01 14:31

    Use this instead:

    document.getElementById('abc').innerText = 'ccc';
    
    0 讨论(0)
  • 2020-12-01 14:32

    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.

    0 讨论(0)
  • 2020-12-01 14:32

    Since TR's innerHTML is read-only as a few people have said, you are better off changing your markup to target the TD:

    <table><tr><td id="changeme"> ... </td></tr></table>
    

    Then you can set the content of the TD as you wish via innerHTML, and change other properties by setting them on the DOM node:

    var td = document.getElementById("changeme");
    td.innerHTML = "New Content";
    td.cssText = "color: red";
    td.className = "highlighted";
    

    You get the idea...

    This saves you the overhead of destroying and creating an extra DOM element (the TD)

    0 讨论(0)
  • 2020-12-01 14:34

    Instead of a <div></div>, you can also use <span></span>

    0 讨论(0)
  • 2020-12-01 14:35

    On another StackOverflow question I found a link to a blog post written by the guy who implemented that part of the Trident engine. It is a design flaw (due to lack of time and backward compatibility issues) that was never fixed. You can read his notes (including his personal workaround) here.

    However, to fix it on my project, since I was already using jQuery, I just used jQuery's .html() function.

    // OLD 
    localRows[cIndex].innerHTML = '<div>Test Div</div>';
    
    // FIXED
    $(localRows[cIndex]).html('<div>Test Div</div>'); 
    
    0 讨论(0)
  • 2020-12-01 14:45

    innerHTML is readonly for as pointed out here: http://msdn.microsoft.com/en-us/library/ms533897%28VS.85%29.aspx

    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.

    Why are you not just putting an idea around the and changing that particular value?

    0 讨论(0)
提交回复
热议问题