求幫忙 無(wú)法運(yùn)行
<!DOCTYPE?html> <html> <head> <meta?charset="UTF-8"> <title>Document</title> <script?type="text/javascript"> ????function?showMes(){ ????????alert("hello?world"); ????} ????var?btn1=document.getElementById("btn1"); ????btn1.attachEvent("onclick",showMes); ????</script> </head> <body> ????<input?type="button"?id="btn1"?value="確定"?name="按鈕1">???? </body> </html>
2016-10-03
你可以把頁(yè)面在360瀏覽器中打開,然后F12調(diào)出開發(fā)人員工具,在Elements標(biāo)簽下會(huì)報(bào)如下錯(cuò)誤:
Uncaught TypeError: btn1.attachEvent is not a function
該錯(cuò)誤是因?yàn)闃?biāo)簽元素還沒有創(chuàng)建就調(diào)用引用標(biāo)簽的腳本就會(huì)出現(xiàn)此問題,將腳本放置在腳本標(biāo)簽的后面就可以了
把js代碼放在input后面,如下:
<input type="button" id="btn1" value="確定" name="按鈕1"> ? ?
<script type="text/javascript">
function showMes(){
alert("hello world");
}
var btn1=document.getElementById("btn1");
btn1.attachEvent("onclick",showMes);
</script>
2016-10-13
你是在谷歌瀏覽器中運(yùn)行的吧 ? attachevent在谷歌中不能運(yùn)行 ?在IE中可以