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.

参考

Appending element is not working in IE11

posted @ 2022-04-07 10:51  灯塔下的守望者  阅读(401)  评论(0编辑  收藏  举报