HTML dom中的insertBefore和Jquery中的区别

来源:互联网 发布:钱程是个什么软件 编辑:程序博客网 时间:2024/06/11 19:37
<!DOCTYPE html><html><body><ul id="myList"><li>Coffee</li><li>Tea</li></ul><p id="demo">请点击按钮向列表插入一个项目。</p><button onclick="myFunction()">试一下</button><script>function myFunction(){var newItem=document.createElement("LI")var textnode=document.createTextNode("Water")newItem.appendChild(textnode)var list=document.getElementById("myList")list.insertBefore(newItem,list.childNodes[0]);}</script><p><b>注释:</b><br>首先请创建一个 LI 节点,<br>然后创建一个文本节点,<br>然后向这个 LI 节点追加文本节点。<br>最后在列表中的首个子节点之前插入此 LI 节点。</p></body></html>


document.getElementById("myList").insertBefore(newItem,existingItem);



jquery中的:

$("button").click(function(){  $("<span>Hello world!</span>").insertBefore("p");});


0 0
原创粉丝点击