关于HTML DOM 中insertBefore与appendChild方法

含义:

insertBefore:在现有的子节点前加入一个新的子节点。
append:在现有的子节点后加入一个新的子节点。

用法:

target.insertBefore(newChild,existingChild);
target是父节点,newChild是要插入到existingChild节点之前的节点。
target.appendChild(newChild);
target是父节点,newChild是要插入到所有子节点之后的节点。

兼容性问题:

在IE下,使用 insertBefore 时,第二个参数,也就是 existingChild 可以被省略。
而在 Chrome 与火狐下,第二个参数必须写上。
Chrome 报错:Uncaught TypeError: Failed to execute ‘insertBefore‘ on ‘Node‘: 2 arguments required, but only 1 present.
火狐报错:TypeError: Not enough arguments to Node.insertBefore

你可能感兴趣的:(关于HTML DOM 中insertBefore与appendChild方法)