有人可以解释为什么这段代码没有阻止html中所有链接的默认行为?我已经尝试了几乎每个组合,但仍然没有禁用链接。我也想在点击链接时更改链接的背景颜色
HTML:
<!doctype html>
<head>
<title>Goal8: Simple Library</title>
</head>
<html>
<body>
<div id="container">
<h1>Goal8: Simple Library</h1>
<a href="http://google.com">Google</a>
<a href="http://yahoo.com">Yahoo</a>
<a href="http://bing.com">Bing</a>
<a href="http://fullsail.com">FullSail</a>
</div>
<script type="text/javascript" src="js/ryu.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</body>
</html>
JS:
(function(){
var ahref= [];
var links = function(){
var urls = document.querySelectorAll("a");
for (var i = 0; i < urls.length; i++){
var aLinks = urls[i].href;
ahref.push(aLinks);
urls[i].onclick = function(e){
this.style.backgroundColor = "#eee";
e.preventDefault();
return false;
}
}
console.log(ahref);
return ahref;
}; links();
})();