為什么document.getElementByTagName沒(méi)法獲取input屬性
<html>
<head>
? ?<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
? ?<title>window對(duì)象</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="點(diǎn)擊我,打開新窗口" />
</form>
</body>
</html>
2022-03-24
陶叫獸表示:你們這是病,得治!如何連接Jquery庫(kù)呢
2015-12-24
2015-12-24
那是因?yàn)槟鉺cript標(biāo)簽放到上面了,瀏覽器是順序加載,所以當(dāng)加載到document.getElementByTagName的時(shí)候還沒(méi)有input標(biāo)簽,所以會(huì)報(bào)錯(cuò),所以你點(diǎn)擊的時(shí)候也沒(méi)反映,解決辦法可以把script標(biāo)簽的放到頁(yè)面最下面,也可以用window.onload。