IE9 createElement并在set操作上设置innerHTML丢弃标签? [英] IE9 createElement and setting innerHTML dropping tags on a set operation?
问题描述
不知道如果我缺少一些明显的东西,因为这是我第一次尝试用原始DOM apis(而不是通过jQuery等等)做的很多。
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).
考虑以下代码,我用document.createElement手动创建一个TBODY,然后设置为innerHTML。
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>
在Chrome和FF中,这个工作原理就像我以为会给我一张桌子等等。在IE9中,innerHTML中的第一个HTML标签似乎被删除。即,不是
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>
我得到
<SPAN>col1</SPAN></TD><TD>col2</TD></TR>
上面的JSFiddle: http://jsfiddle.net/pAJwu/
A JSFiddle of the above: http://jsfiddle.net/pAJwu/
任何想法这里发生了什么?
Any idea what's going on here?
推荐答案
IE到版本9不支持在某些元素上设置 innerHTML
这就是为什么;该链接还包括可能的解决方法。
IE up to version 9 doesn't support setting innerHTML
on some elements. Here's why; the link also includes possible workaround.
这篇关于IE9 createElement并在set操作上设置innerHTML丢弃标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!