[英]Switch Case does not respond to initialized function
我最近正在練習 JS 書中的一個例子,即使復制了這個例子,我也無法達到預期的結果
這是 HTML:
<form action="#">
<input type="button" value="Lincoln" id="Lincoln">
<input type="button" id="Kennedy" value="Kennedy">
<input type="button" id="Nixon" value="Nixon">
</form>
這是JS
window.onload = initAll;
function initAll() {
document.getElementById("Lincoln").onclick = saySomething;
document.getElementById("Kennedy").onclick = saySomething;
document.getElementById("Nixon").onclick = saySomething;
}
function saySomething() {
switch(this.id) {
case "Lincoln":
alert("Four score and seven years ago...");
break;
case "Kennedy":
alert("Ask not what your country can do for you...");
break;
case "Nixon":
alert("I am not a crook!");
break;
default:
}
}
我正在嘗試在本地運行代碼,但它在這里不起作用我知道代碼沒有問題,但為什么它不在本地運行。
好的,我知道了,我將腳本外部鏈接代碼放在頁腳中,現在可以正常工作了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.