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;
<!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;
相关文章推荐
- js+css实现回到顶部按钮(back to top)
- BackToTop.js 为你的网站添加“回到顶部”按钮
- Back Top ①(回到顶部)原生JS实现
- js+css实现回到顶部按钮(back to top)
- JS实现回到Top(顶部)--JavaScrip
- js+css实现回到顶部按钮(back to top)
- JS实现回到Top(顶部)--JavaScript
- BackToTop.js 为你的网站添加“回到顶部”按钮
- Js动态添加事件 实现附加自己的事件属性
- js实现超过页面一屏后,点击图标滚动到页面顶部top
- 百分百纯js实现回到顶部按钮
- JS实现下拉框的动态添加(附效果)
- JS自定义功能函数实现动态添加网址参数修改网址参数值
- JS实现动态表格 添加行 删除行 删除列
- 如何实现将JS动态添加的TR中的值传到后台中并批量插入
- JS函数实现动态添加CSS样式表文件
- jQuery插件scrollToTop 4行代码实现网站回到顶部
- js取得url参数,js回到顶部代码,动态加载JS文件
- 使用js实现对table的动态添加、删除和更新
- js实现动态添加,删除行,onkeyup表格求和