<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>window对象</title>
<script type="text/javascript">
var button=document.getElementsByTagName("input")[0];
button.onclick=function (){
window.open("http://www.baidu.com","_black","height=400px width=600px ")
};
</script>
</head>
<body>
<form>
<input type="button" value="点击我,打开新窗口" />
</form>
</body>
</html>
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>window对象</title> </head> <body> <form> <input type="button" value="点击我,打开新窗口" /> </form> </body> <script type="text/javascript"> window.onload = function(){ var button=document.getElementsByTagName("input")[0]; button.onclick=function (){ window.open("http://www.baidu.com","_black","height=400px width=600px ") }; } </script> </html>
那是因为你script标签放到上面了,浏览器是顺序加载,所以当加载到document.getElementByTagName的时候还没有input标签,所以会报错,所以你点击的时候也没反映,解决办法可以把script标签的放到页面最下面,也可以用window.onload。