javascript - 为什么此代码以前工作时不起作用并为随机值添加按钮

标签 javascript

该代码之前运行良好。我尝试了很多方法来修复它,但它似乎不起作用。是因为变数太多了吗?此外,我想添加一个按钮,并在放置“随机”按钮时显示值(数组)。谢谢!

<html><head>
<title>
  Mad Libs Story
</title>

<script>

function getVars() {

    var firstPerson = String(document.getElementbyId("personOne").value);
    var firstAdjective = String(document.getElementById("adjectiveOne").value);
    var secondAdjective = String(document.getElementById("adjectiveTwo").value);
    var thirdAdjective = String(document.getElementById("adjectiveThree").value);
    var secondPerson = String(document.getElementById("personTwo").value);
    var fourthAdjective = String(document.getElementById("adjectiveFour").value);
    var firstNumber = Number(document.getElementById("numberOne").value);
    var thirdPerson = String(document.getElementById("personThree").value);

document.getElementById("madLibCreation").innerHTML = "Dear " + firstPerson + ",Overall, the camp is " + firstAdjective + "The camp counselors are " + secondAdjective + "and the food is " + thirdAdjective + ".Today, I met someone named " + secondPerson + "and we become " + fourthAdjective + "friends. I hope to write to you in " + firstNumber + "days.Sincerely," + thirdPerson + ".";
} 
</script>
</head>
<body>
<h3>
Welcome to Mad Libs! Please type in the prompted Information. Then press the submit button. Have fun!   
</h3>  

<p>
  Name of Person in Room: <input type="text" id="personOne">
</p>

<p>
  Adjective: <input type="text" id="adjectiveOne">
  </p>

 <p>
   Adjective: <input type="text" id="adjectiveTwo">
  </p>

  <p>
    Adjective: <input type="text" id="adjectiveThree">
  </p>

  <p>
    Name of Someone: <input type="text" id="personTwo">
  </p>  

  <p>
    Adjective: <input type="text" id="adjectiveFour">
  </p>

  <p>
    Number: <input type="text" id="numberOne">
  </p>

  <p>
   Name of Someone: <input type="text" id="personThree">
  </p>
<p>

<input type="submit" value="Get My MadLib Creation!" onclick="getVars();">
  </p>
<p id="madLibCreation"></p>


</body></html>

最佳答案

var firstPerson = String(document.getElementbyId("personOne").value); 中存在一个小拼写错误。 getElementbyId 需要大写 B:getElementById。这是类型已更正的代码。

<html><head>
<title>
  Mad Libs Story
</title>

<script>

function getVars() {

    var firstPerson = String(document.getElementById("personOne").value);
    var firstAdjective = String(document.getElementById("adjectiveOne").value);
    var secondAdjective = String(document.getElementById("adjectiveTwo").value);
    var thirdAdjective = String(document.getElementById("adjectiveThree").value);
    var secondPerson = String(document.getElementById("personTwo").value);
    var fourthAdjective = String(document.getElementById("adjectiveFour").value);
    var firstNumber = Number(document.getElementById("numberOne").value);
    var thirdPerson = String(document.getElementById("personThree").value);

document.getElementById("madLibCreation").innerHTML = "Dear " + firstPerson + ",Overall, the camp is " + firstAdjective + "The camp counselors are " + secondAdjective + "and the food is " + thirdAdjective + ".Today, I met someone named " + secondPerson + "and we become " + fourthAdjective + "friends. I hope to write to you in " + firstNumber + "days.Sincerely," + thirdPerson + ".";
} 
</script>
</head>
<body>
<h3>
Welcome to Mad Libs! Please type in the prompted Information. Then press the submit button. Have fun!   
</h3>  

<p>
  Name of Person in Room: <input type="text" id="personOne">
</p>

<p>
  Adjective: <input type="text" id="adjectiveOne">
  </p>

 <p>
   Adjective: <input type="text" id="adjectiveTwo">
  </p>

  <p>
    Adjective: <input type="text" id="adjectiveThree">
  </p>

  <p>
    Name of Someone: <input type="text" id="personTwo">
  </p>  

  <p>
    Adjective: <input type="text" id="adjectiveFour">
  </p>

  <p>
    Number: <input type="text" id="numberOne">
  </p>

  <p>
   Name of Someone: <input type="text" id="personThree">
  </p>
<p>

<input type="submit" value="Get My MadLib Creation!" onclick="getVars();">
  </p>
<p id="madLibCreation"></p>


</body></html>

关于javascript - 为什么此代码以前工作时不起作用并为随机值添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59483084/

相关文章:

javascript - 按类查找 Angular 元素的最佳方法是什么?

javascript - 如何使用正则表达式查找对象成员名称

javascript - 如果用户使用的是 IE 9 及更低版本,则使用 jQuery 显示 div

javascript - 设置 mime 类型或运行本地媒体播放器

javascript - 默认选择选项

javascript - 我如何创建一个带有 2 个键的 JS 对象

javascript instanceof 从字符串名称获取类型

javascript - 在点击事件中分配点击事件处理程序

javascript - 谷歌地图 API : Total distance with waypoints

javascript - 是否可以在构造函数之外定义特权方法?