关于js的onclick事件的问题?
发布网友
发布时间:2022-04-22 14:23
我来回答
共1个回答
热心网友
时间:2023-09-22 11:22
两处有错误:
1、var links = getElementsByTagName("a");
改为:var links = document.getElementsByTagName("a");
2、link[i].onclick 改为:links[i].onclick;
修改后完整代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>2.html</title>
</head>
<body>
<a href="http://www.baidu.com">Example</a>
<script>
window.onload = prepareLinks;
function prepareLinks(){
var links = document.getElementsByTagName("a");
for(var i = 0;i < links.length;i++){
if(links[i].getAttribute("class") == "popup"){
links[i].onclick = function(){
popup(this.getAttribute("href"));
return false;
}
}
}
}
function popup(winurl){
window.open(winurl,"popup","width:100,height:100");
}
</script>
</body>
</html>追问那为什么我在最后用alert()他还是可以弹出窗口啊