Javascript函数是';没有反应
我有所有的代码,就像为我的类示例设置的那样,但当我在浏览器中打开并单击底部的“生成故事”按钮时,它不会弹出任何内容。 此外,对于“outputDiv.innerHTML”,我的“innerHTML”没有颜色。不知道我做错了什么Javascript函数是';没有反应,javascript,html,function,Javascript,Html,Function,我有所有的代码,就像为我的类示例设置的那样,但当我在浏览器中打开并单击底部的“生成故事”按钮时,它不会弹出任何内容。 此外,对于“outputDiv.innerHTML”,我的“innerHTML”没有颜色。不知道我做错了什么 <!DOCTYPE html> <html> <head> <title> Story Time</title> <script type="text/javascript&q
<!DOCTYPE html>
<html>
<head>
<title> Story Time</title>
<script type="text/javascript">
function GenerateStory() {
outputDiv.innerHTML='Once upon a time there was a boy named' + NameBox.value + 'that lived in' + CityBox.value + 'with his' + numberbox.value + 'siblings.' + NameBox.value + 'had a dream of being a' + OccupationBox.value + 'so he could' + HobbyBox.value + 'whenever he wanted and move to' + CityBox2.value 'when he grew up.'
}
</script>
</head>
<body>
<h1>Story Time</h1>
<table style="margin-left: auto; margin-right: auto;">
<tr><td>Name:</td>
<td><input type="text" id="NameBox" value="" size="20"></td></tr>
<tr><td>City:</td>
<td><input type="text" id="CityBox" value="" size="20"></td></tr>
<tr><td>Number Between 2-100: </td>
<td><input type="text" id="NumberBox" value="" size="20"></td></tr>
<tr><td>Occupation:</td>
<td><input type="text" id="OccupationBox" value="" size="20"></td> </tr>
<tr><td>Hobby:</td>
<td><input type="text" id="HobbyBox" value="" size="20"></td></tr>
<tr><td>Another City:</td>
<td><input type="text" id="CityBox2" value="" size="20"></td></tr>
</table>
<hr>
<button onclick="GenerateStory();">Click for the Story</button>
<hr>
<div id="outputDiv">
</div>
</body>
</html>
故事时间
函数GenerateStory(){
outputDiv.innerHTML='从前,有一个名叫'+NameBox.value+'的男孩和他的'+numberbox.value+'兄弟姐妹住在'+CityBox.value+'里。'+NameBox.value+'有一个梦想,就是成为一个'+occuptionbox.value+',所以他可以随时'+hobbox.value+',长大后搬到'+CityBox2.value'
}
故事时间
姓名:
城市:
2-100之间的数字:
职业:
爱好:
另一个城市:
点击查看故事
看起来您可能只需要通过其id获取元素:
嗨,检查一下,我已经构建了你的代码。检查这是否有效。您尚未从文档中拾取元素
故事时间
故事时间
姓名:
城市:
2-100之间的数字:
职业:
爱好:
另一个城市:
点击查看故事
函数GenerateStory(){
让NameBox=document.getElementById(“NameBox”).value;
让cityBox=document.getElementById(“cityBox”).value;
让NumberBox=document.getElementById(“NumberBox”).value;
让OccupationBox=document.getElementById(“OccupationBox”).value;
让HobbyBox=document.getElementById(“HobbyBox”).value;
让CityBox2=document.getElementById(“CityBox2”).value;
document.getElementById(“outputDiv”).innerHTML=
“从前,有一个男孩名叫”+
名称框+
“生活在”+
城厢+
“用他的”+
号码盒+
“兄弟姐妹。”+
名称框+
“梦想成为一名+
职业箱+
“所以他可以”+
哈比盒子+
“他想什么时候就什么时候搬”+
CityBox2+
“等他长大了。”;
}
打开开发者控制台,以便查看错误消息。在GenerateStory
中缺少+
,因此未定义函数。而numberbox.value
应该是numberbox.value
…所以这是一个小的打字错误问题。一定要用一根短绒。这就是你能抓住最简单错误的方法。谢谢你,这很好。函数对我来说是一个很难理解的概念,我刚刚发布了另一个函数问题,如果你想在这个问题上付出你的两分钱,当然,我会调查的谢谢!!我把这一点和对这个问题的另一个回答结合起来。再次感谢你!
var outputDiv = document.getElementById('outputDiv');