IE9 createElement and setting innerHTML dropping tags on a set operation?

走远了吗. 提交于 2019-12-07 04:56:16

问题


Not sure if I am missing something obvious, as this is the first time I've tried to do much with raw DOM apis (as opposed to via jQuery, or such).

Consider the following code, where I manually create a TBODY with document.createElement and then set it's innerHTML.

<table id="myTable">
</table>

<script type='text/javascript'>
    var row = "<tr><td><span>col1</span></td><td>col2</td></tr>";
    var render = function(){
        var table = document.getElementById('myTable');
        var tbody = document.createElement('tbody');
        tbody.innerHTML = row;
        table.appendChild(tbody);

        console.log(tbody.innerHTML);
    };

    $(document).ready(function(){
        render();
    });
</script>

In Chrome and FF, this works as I thought it would - giving me a table, etc. However, in IE9, it seems the first HTML tags in the innerHTML are being dropped. i.e., instead of

<TR><TD><SPAN>col1</SPAN></TD><TD>col2</TD></TR>

I get

<SPAN>col1</SPAN></TD><TD>col2</TD></TR>

A JSFiddle of the above: http://jsfiddle.net/pAJwu/

Any idea what's going on here?


回答1:


IE up to version 9 doesn't support setting innerHTML on some elements. Here's why; the link also includes possible workaround.



来源:https://stackoverflow.com/questions/8097139/ie9-createelement-and-setting-innerhtml-dropping-tags-on-a-set-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!