求幫忙 無法運行
<!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
你可以把頁面在360瀏覽器中打開,然后F12調出開發人員工具,在Elements標簽下會報如下錯誤:
Uncaught TypeError: btn1.attachEvent is not a function
該錯誤是因為標簽元素還沒有創建就調用引用標簽的腳本就會出現此問題,將腳本放置在腳本標簽的后面就可以了
把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
你是在谷歌瀏覽器中運行的吧 ? attachevent在谷歌中不能運行 ?在IE中可以