javscript为网站博客添加menu列表

来源:互联网 发布:网络大电影演员 编辑:程序博客网 时间:2024/05/19 19:29

All blogs(h1标记)

Blog1(h2标记)

World contains all kinds of elements, in colour and form, just like the keyboard contains the notes of all music.For the artist is responsible to pick, and choose, and group with science, these elements, that the result may be magic.(p标记)

Blog2(h2标记)

World contains all kinds of elements, in colour and form, just like the keyboard contains the notes of all music.For the artist is responsible to pick, and choose, and group with science, these elements, that the result may be magic.(p标记)

Blog3(h2标记)

World contains all kinds of elements, in colour and form, just like the keyboard contains the notes of all music.For the artist is responsible to pick, and choose, and group with science, these elements, that the result may be magic.(p标记)

Blog4(h2标记)

World contains all kinds of elements, in colour and form, just like the keyboard contains the notes of all music.For the artist is responsible to pick, and choose, and group with science, these elements, that the result may be magic.(p标记)

Blog5(h2标记)

World contains all kinds of elements, in colour and form, just like the keyboard contains the notes of all music.For the artist is responsible to pick, and choose, and group with science, these elements, that the result may be magic.(p标记)

Blog6(h2标记)

World contains all kinds of elements, in colour and form, just like the keyboard contains the notes of all music.For the artist is responsible to pick, and choose, and group with science, these elements, that the result may be magic.(p标记)

为网站博客添加menu列表,将Blog1 Blog2 Blog3标题排列在上方...

<script>
function makeMenu(){
var h2s=document.getElementsByTagName("h2");
var menu=document.createElement("div");
var menuUl=document.createElement("ul");
menu.appendChild(menuUl);

for(var i=0;i<h2s.length;i++){
var itemText=h2s[i].childNodes[0].nodeValue;

var menuLi=document.createElement("li");
menuUl.appendChild(menuLi);

var menuLiA=document.createElement("a");
menuLi.appendChild(menuLiA);
menuLiA.setAttribute("href","#item"+i);

var menuText=document.createTextNode(itemText);
menuLiA.appendChild(menuText);

var anc=document.createElement("a");
anc.setAttribute("name","item"+i);
document.body.insertBefore(anc,h2s[i]);
}
document.body.insertBefore(menu,document.body.firstChild);
}
window.onload=makeMenu;
</script>

 

0 0