JavaScript判断按钮URL是否指向当前页
html
标签内容<div id="navigation"> <ul> <li><a href="index.html">Home</a></li> <li><a href="about.html">About</a></li> <li><a href="photos.html">Photos</a></li> <li><a href="live.html">Live</a></li> <li><a href="contact.html">Contact</a></li> </ul> </div>
JavaScript 代码
function highlightPage() { if (!document.getElementsByTagName) return false; if (!document.getElementById) return false; if (!document.getElementById("navigation")) return false; var nav = document.getElementById("navigation"); var links = nav.getElementsByTagName("a"); for (var i=0; i<links.length; i++) { var linkurl = links[i].getAttribute("href"); var currenturl = window.location.href; if (currenturl.indexOf(linkurl) != -1) { links[i].className = "here"; var linktext = links[i].lastChild.nodeValue.toLowerCase(); document.body.setAttribute("id",linktext); } } }