IE9 createElement 和设置innerHTML 在设置操作上放置标签?
不确定我是否遗漏了一些明显的东西,因为这是我第一次尝试使用原始 DOM api(而不是通过 jQuery 等)做很多事情。
考虑以下代码,我在其中使用 document.createElement 手动创建 TBODY,然后设置它的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 标签似乎被删除了。即,而不是
<TR><TD><SPAN>col1</SPAN></TD><TD>col2</TD></TR>
我得到
<SPAN>col1</SPAN></TD><TD>col2</TD></TR>
上述的 JSFiddle: http://jsfiddle.net/pAJwu/
任何想法这是怎么回事?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.