当 添加名称时,ID“nameGreet”上没有任何内容显示。
HTML: http://pastebin.com/TmrnYsRz
JavaScript的: http://pastebin.com/7ScPRCS9
if (window.confirm("Would you like to enter a name? This gives the site some personalized options?") === true) {
var name = window.prompt("Name:");
document.getElementById("nameGreet").innerHTML = "name";
}
<h1>George B</h1>
<p id="nameGreet"> </p>
答案 0 :(得分:1)
由于您引用"name"
,您将逐字地显示该字符串,而不是使用该变量。
if (window.confirm("Would you like to enter a name? This gives the site some personalized options?") === true) {
var name = window.prompt("Name:");
document.getElementById("nameGreet").innerHTML = name;
}
<h1>George B</h1>
<p id="nameGreet"> </p>