2017-08-31 70 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();