您的位置:首页 > Web前端 > JavaScript

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

2017-06-24 19:51 417 查看
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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: