现代浏览器实现了以下有用的方法:
.prepend()
.append()
.before()
.after()
这是一个受欢迎的补充。
他们还实现了.insertAdjacentElement()
,据我所知,这将达到相同的结果,尽管更冗长一些。
除了语法之外,较新的方法和(稍(旧的.insertAdjacentElement()
之间是否存在实际差异?
我知道较新的方法在Microsoft浏览器中尚不可用,但即使在IE8中也可用.insertAdjacentElement()
,因此polyfill应该不难。
据我所知,除了语法之外没有显着差异。
新方法可以采用多个参数,其中每个参数可以是节点或字符串。insertAdjacentElement
采用位置(字符串(和节点。如果要插入多个节点,则必须多次调用它。如果要插入文本,则必须改用insertAdjacentText
。
该规范是我能找到的最佳来源:
- https://dom.spec.whatwg.org/#dom-parentnode-prepend
- https://dom.spec.whatwg.org/#dom-element-insertadjacentelement