<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>01重定向</title>
<script type="text/javascript">
window.onload=init;
function init(){
document.getElementById("link").onclick=redirect;
}
function redirect(){
alert("重定向页面");
window.location="http://www.baidu.com";
return false;
}
</script>
</head>
<body>
<a href="test.html" title="重定向" id="link">链接重定向</a>
</body>
</html>
一个简单的重定向操作而已
问:为何要用到window.onload 如果不用onload而直接使用document.getElementById("link").onclick=redirect;确实无法实现重定向的功能。这是为什么呢?
本来就一个onclick事件,不是说在单击的时候就能触发redirect()吗?为什么还要先在onload的时候装载啊?
初学者,首次提问