JS动态实现:每个h2后添加top回到顶部

来源:互联网 发布:淘宝直播东西是真的吗 编辑:程序博客网 时间:2024/06/06 02:38

html:

<!DOCTYPE html><html><head><title>Char 12 test</title><meta charset="utf-8"><script src="after.js"></script><!--<script type="text/javascript">window.onload = insertAfter();</script>--></head><body><div id="top"></div><h2 id="hh">first</h2><p id="pp">123bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cook well]a person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur</p><h2>second</h2><p>erson who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cook well]a person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who dle running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Onoes something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only am</p><h2>third</h2><p>erson who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cook well]a person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who dle running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Onoes something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only amateurs make this kind of mistake.He's a mere amateur when it comes to cooking. [=he doesn't know how to cooka person who does something poorly : a person who is not skillful at a job or other activity.The people running that company are a bunch of amateurs.Only am</p><script type="text/javascript">// var n = document.createElement("a");// var text = document.createTextNode("link");// n.appendChild(text);// n.setAttribute("href","www.baidu.com");// n.setAttribute("value","链接");// var pV= document.getElementById("hh");//console.log(pV);//insertAfter(n,pV);window.onload= function(){// insertAfter(n,pV);Totop();}</script></body></html>
JS文件:after.js

function insertAfter(newNode,ObjNode){        console.log(ObjNode);var parent = ObjNode.parentNode;console.log(parent);console.log(parent.lastChild);if(ObjNode == parent.lastChild){parent.appendChild(newNode);} else {console.log(ObjNode.nextSibling);parent.insertBefore(newNode,ObjNode.nextSibling);}}function Totop(){var h2s = document.getElementsByTagName("h2");for(var i=0 ; i<h2s.length ; i++){var a = document.createElement("a");var text = document.createTextNode("top");a.appendChild(text);a.setAttribute("href","#top");insertAfter(a,h2s[i]);}}window.onload = Totop;





原创粉丝点击