记录今天在插入节点问题上出现了报错
Uncaught TypeError: Failed to execute ‘insertBefore‘ on ‘Node‘: 2 arguments required, but only 1 present.
在之前都是用appendChild,突发想用insertBefore,就报错了,感觉去看了一下文档。
-
parentNode.insertBefore(newnode,existingnode):在现有的子节点前加入一个新的子节点。
parentNode:父节点,也就是你要插入的地方
newnode:你要插入的新节点
existingnode:你要插入哪个节点的前面 -
node.appendChild(newnode):在现有的子节点后加入一个新的子节点。
在网上找了一下原因,是兼容性问题:
在 Chrome 与火狐下,第二个参数必须写上。
Chrome 报错:
Uncaught TypeError: Failed to execute ‘insertBefore‘ on ‘Node‘: 2 arguments required, but only 1 present.
火狐报错:
TypeError: Not enough arguments to Node.insertBefore