gpt4 book ai didi

javascript - 一个简单的 JS madlib 程序无法运行

转载 作者:行者123 更新时间:2023-12-03 06:54:20 25 4
gpt4 key购买 nike

我用 JS 和 bootstrap 编写了一个 Madlib 程序。当用户输入值时,完整的故事将出现在引导模式中。但这不起作用。该模式显示为全空。请帮我找出问题所在。代码如下:

<!DOCTYPE html>

<html lang="en">
<head>
<title>A simple Madlib game</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">A simple MadLib game</h4>
</div>
<div class="modal-body">
<div id="story_div"></div>
</div>
</div>
</div>
</div>
<div class="container">
<header>
<h2 class="text-center">A simple JavaScript MadLib game</h2>
</header>
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<div class="form-horizontal">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">An adjective</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="adjOne" placeholder="such as: large, rusty, old etc.">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">An animal name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="animalOne" placeholder="such as: cow, monkey, lion etc.">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">A verb (Past tense)</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="verbPastOne" placeholder="such as: laughed, walked etc.">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">An adverb</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="adverbOne" placeholder="such as: always, carefully, barefooted etc.">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Another adjective</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="adjTwo" placeholder="such as: large, rusty, old etc.">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">A noun</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="noun" placeholder="">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">A noun</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="nounTwo" placeholder="">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">An adjective</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="adjThree" placeholder="such as: large, rusty, old etc.">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">A verb</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="verb" placeholder="such as: always, carefully, barefooted etc.">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">An adverb</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="adverbTwo" placeholder="such as: always, carefully, barefooted etc.">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">A verb (Past tense)</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="verbPastTwo" placeholder="such as: laughed, walked etc.">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">An adjective</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="adjFour" placeholder="such as: large, rusty, old etc.">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="button" id="launch" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Launch story</button>
</div>
</div>
</div>
</div>
</div>
</div>




<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script>
window.onload = function(){
var adjOne = document.querySelector("#adjOne");
var animalOne = document.querySelector("#animalOne");
var verbPastOne = document.querySelector("#verbPastOne");
var adverbOne = document.querySelector("#adverbOne");
var adjTwo = document.querySelector("#adjTwo");
var noun = document.querySelector("#noun");
var nounTwo = document.querySelector("#nounTwo");
var adjThree = document.querySelector("#adjThree");
var verb = document.querySelector("#verb");
var adverbTwo = document.querySelector("#adverbTwo");
var verbPastTwo = document.querySelector("#verbPastTwo");
var adjFour = document.querySelector("#adjFour");
var launch = document.querySelector("#launch");
launch.addEventListener("click", writeStory, false);

function writeStory(){
launchedStory = "";
launchedStory += "<p>Today I went to the zoo. I saw a(n) " + adjOne.value + " " + animalOne.value + " jumping up and down in its tree.<p>";
launchedStory += "<p>He " + verbPastOne.value + " " + adverbOne.value + " through the large tunnel that led to its " + adjTwo.value + " " + noun.value + ".<p>";
launchedStory += "<p>I got some peanuts and passed them through the cage to a gigantic gray " + nounTwo.value + " towering above my head. Feeding that animal made me hungry<p>";
launchedStory += "<p>I went to get a " + adjThree.value + " scoop of ice cream. It filled my stomach.<p>";
launchedStory += "<p>Afterwards I had to " + verb.value + " " + adverbTwo.value + " to catch our bus.<p>";
launchedStory += "<p>When I got home I " + verbPastTwo.value + " my mom for a " + adjFour.value + " day at the zoo.<p>";

story_div.innerHTML = launchedStory;
}
}
</script>
</body>

</html>

最佳答案

如果您查看了 JavaScript 控制台,您会直接看到问题(在您最喜欢的浏览器上尝试F12)。

它们都是由于拼写错误造成的。

  1. 写成"#verbPastOne"而不是"#verPastOne"
  2. 编写 "#verbPastTwo" 而不是 "#verPastTwo"

最后但并非最不重要的一点...

  • 编写 verbPastOne.value 而不是 verbPastOne.Value
  • 是的,JavaScript 区分大小写。

    关于javascript - 一个简单的 JS madlib 程序无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37350020/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com