admin管理员组文章数量:1022695
In this example says that second parameter for insertBefore() method is optional:
The child node you want to insert the new node before. When not specified, the insertBefore method will insert the newnode at the end.
My code:
let li = document.createElement('LI');
li.appendChild(document.createTextNode("Water"));
let list = document.getElementById('list');
list.insertBefore(li);
<ul id="list">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
In this example says that second parameter for insertBefore() method is optional:
The child node you want to insert the new node before. When not specified, the insertBefore method will insert the newnode at the end.
My code:
let li = document.createElement('LI');
li.appendChild(document.createTextNode("Water"));
let list = document.getElementById('list');
list.insertBefore(li);
<ul id="list">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
But when I try to use insertBefore with one parameter I have an error:
Uncaught TypeError: Failed to execute 'insertBefore' on 'Node': 2 arguments required, but only 1 present.
But this code work correctly:
list.insertBefore(li, list.childNodes[0])
Share
Improve this question
asked May 9, 2018 at 9:42
Eugene ZalivadnyiEugene Zalivadnyi
4777 silver badges15 bronze badges
2 Answers
Reset to default 3In this example says that second parameter for insertBefore() method is optional:
It's incorrect, see MDN or the spec (though frankly that current spec is much harder to follow than the old one). (Unfortunately, accuracy and pleteness are ongoing issues with w3schools, strongly remend using MDN instead.) You can use null
as the second argument, but you must provide it:
let li = document.createElement('LI');
li.appendChild(document.createTextNode("Water"));
let list = document.getElementById('list');
list.insertBefore(li, null);
<ul id="list">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
w3schools is wrong yet again. MDN is much more reliable:
insertBefore
referenceNode is not an optional parameter -- you must explicitly pass a Node or null. Failing to provide it or passing invalid values may behave differently in different browser versions.
In this example says that second parameter for insertBefore() method is optional:
The child node you want to insert the new node before. When not specified, the insertBefore method will insert the newnode at the end.
My code:
let li = document.createElement('LI');
li.appendChild(document.createTextNode("Water"));
let list = document.getElementById('list');
list.insertBefore(li);
<ul id="list">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
In this example says that second parameter for insertBefore() method is optional:
The child node you want to insert the new node before. When not specified, the insertBefore method will insert the newnode at the end.
My code:
let li = document.createElement('LI');
li.appendChild(document.createTextNode("Water"));
let list = document.getElementById('list');
list.insertBefore(li);
<ul id="list">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
But when I try to use insertBefore with one parameter I have an error:
Uncaught TypeError: Failed to execute 'insertBefore' on 'Node': 2 arguments required, but only 1 present.
But this code work correctly:
list.insertBefore(li, list.childNodes[0])
Share
Improve this question
asked May 9, 2018 at 9:42
Eugene ZalivadnyiEugene Zalivadnyi
4777 silver badges15 bronze badges
2 Answers
Reset to default 3In this example says that second parameter for insertBefore() method is optional:
It's incorrect, see MDN or the spec (though frankly that current spec is much harder to follow than the old one). (Unfortunately, accuracy and pleteness are ongoing issues with w3schools, strongly remend using MDN instead.) You can use null
as the second argument, but you must provide it:
let li = document.createElement('LI');
li.appendChild(document.createTextNode("Water"));
let list = document.getElementById('list');
list.insertBefore(li, null);
<ul id="list">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
w3schools is wrong yet again. MDN is much more reliable:
insertBefore
referenceNode is not an optional parameter -- you must explicitly pass a Node or null. Failing to provide it or passing invalid values may behave differently in different browser versions.
本文标签: javascriptHow to use insertBefore() without second parameterStack Overflow
版权声明:本文标题:javascript - How to use insertBefore() without second parameter? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745516938a2154113.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论