简体   繁体   English

为什么这个生成器代码在 JavaScript 中不起作用?

[英]Why does this genorater code not work in JavaScript?

I am trying to create a simple genorator code for a flight simulator.我正在尝试为飞行模拟器创建一个简单的生成器代码。 It should genorate four numbers between 0 and 7, and then update the text of a Div.它应该生成 0 到 7 之间的四个数字,然后更新 Div 的文本。

HTML: HTML:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>SG 0.0.0</title>
    <script src="index.js"></script>
</head>
<body>
    <div id="squawk">0000</div>
    <button onclick="genSquawk" id="gen">Genorate Squawk</button>
</body>
</html>

JS: JS:

const div = document.getElementById('squawk')

var num1 = Math.random() * 7;
var num2 = Math.random() * 7;
var num3 = Math.random() * 7;
var num4 = Math.random() * 7;

function genSquawk() {
    div.textContent = num1,num2,num3,num4;
}

div.textContent = num1,num2,num3,num4; is not valid syntax.不是有效的语法。 If you want an an array, for instance, you could use [num1, num2, num3, num4] .例如,如果你想要一个数组,你可以使用[num1, num2, num3, num4] Since you're updating the node's text content, you likely want to make these numbers a string value to insert into your div element.由于您正在更新节点的文本内容,您可能希望将这些数字作为字符串值插入到您的div元素中。 In that case, there are many ways to make a string from your series of numbers.在这种情况下,有很多方法可以从您的一系列数字中创建一个字符串。 One way is string concatenation , something like一种方法是字符串连接,例如

div.textContent = `${num1}, ${num2}, ${num3}, ${num4}`;

There are a few changes to be made.需要进行一些更改。 One is the following line:一个是以下行:

<button onclick="genSquawk" id="gen">Genorate Squawk</button>

You are referencing the genSquawk function on the onclick property, not calling it.您在onclick属性上引用genSquawk function,而不是调用它。 The correct is:正确的是:

<button onclick="genSquawk()" id="gen">Genorate Squawk</button>

Now it should trigger the function properly.现在它应该正确触发 function。 The second one is: the syntax num1,num2,num3,num4 is not correct, and will cause the code to display only the first num.第二个是:语法num1,num2,num3,num4不正确,会导致代码只显示第一个num。 In order to do so, use string literals: ${num1}, ${num2}, ${num3}, ${num4} .为此,请使用字符串文字: ${num1}, ${num2}, ${num3}, ${num4}

Finally, I do not know if you want integers or if each of the four numbers could be floats.最后,我不知道您是否想要整数,或者四个数字中的每一个是否都可以是浮点数。 But I added a function to generate a int number between 0 and 7.但是我添加了一个 function 来生成 0 到 7 之间的 int 数。

 function getRandomArbitrary() { return Math.floor(Math.random() * 7); } function genSquawk() { const div = document.getElementById('squawk') var num1 = getRandomArbitrary(); var num2 = getRandomArbitrary(); var num3 = getRandomArbitrary(); var num4 = getRandomArbitrary(); div.textContent = `${num1}${num2}${num3}${num4}`; }
 <,DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width. initial-scale=1"> <title>SG 0.0.0</title> <script src="index.js"></script> </head> <body> <div id="squawk">0000</div> <button onclick="genSquawk()" id="gen">Genorate Squawk</button> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM