IE11的append失效问题解决方案
问题
一个很简单的动态创建 select
,然后添加 option
节点的代码,一直运行没问题,结果 ie11
却报错了。
<script type="text/javascript">
var expressType = document.createElement("select");
var option = document.createElement("option");
$(option).text('请选择');
expressType.append(option);
</script>
解决
查了下资料,原来 ie
不支持 element.append
方法,但是 element.appendChild
方法确实所有浏览器通用的。
修改后
<script type="text/javascript">
var expressType = document.createElement("select");
var option = document.createElement("option");
$(option).text('请选择');
expressType.appendChild(option);
</script>
切记~
IE11 (at least my version or settings) did not support element.append. It did, however, work as expected with element.appendChild.
The browser compatibility section of MDN confirms that IE (in contrast to every other browser) has never had ParentNode.append support but has always had Node.appendChild support, which appears to be universal.