2017-08-31 74 views
0
var yourName; //global variable accessible to all functions 
function showAnotherMessage() { 
    alert("Hi " + yourName ".\nThis is an alert message is no longer defined\nin the HTML but in a JavaScript file"); 
} 

function init() { 
    yourName = Prompt("Hi. Enter your name.\nWhen the browser window is first loaded\nthe function containing this prompt window is called.", "Your name"); 
    clickme = document.getElementById("clickme"); 
    clickme.onclick = showAnotherMessage; 

    } 

window.onload = init(); 
+4

請顯示[MCVE],解釋什麼行代碼有語法錯誤。 – 4castle

回答

0

通到window.onload不是結果的功能,但功能的參考,並添加警報的消息中錯過了+

alert("Hi " + yourName + ".\nThis is an alert message is no longer defined\nin the HTML but in a JavaScript file"); 

參考

window.onload = init;

+0

雖然這不會是一個語法錯誤。 – 4castle

+0

必須是別的東西!因爲錯誤仍然存​​在 –

2
function showAnotherMessage() { 
    alert("Hi " + yourName + ".\nThis is an alert message is no longer defined\nin the HTML but in a JavaScript file"); 
} 

你錯過了「+」的提醒。

0

您在提醒並置中缺少+alert("Hi " + yourName + ".\nThis is an alert message is no longer defined\nin the HTML but in a JavaScript file");

0

您錯過了一個連接。 提示應該是小寫。

var yourName; //global variable accessible to all functions 
function showAnotherMessage() { 
    alert("Hi " + yourName + ".\nThis is an alert message is no longer defined\nin the HTML but in a JavaScript file"); 
} 

function init() { 
    yourName = prompt("Hi. Enter your name.\nWhen the browser window is first loaded\nthe function containing this prompt window is called.", "Your name"); 
    clickme = document.getElementById("clickme"); 
    clickme.onclick = showAnotherMessage; 

    } 

window.onload = init(); 
0

如果問題仍然存在,,則與調用函數

clickme.onclick = showAnotherMessage; 
// instead use below 
clickme.onclick = showAnotherMessage();