简体   繁体   English

我正在尝试制作一个将两个单词混合在一起的程序,但是当我添加 function 时它就停止工作了

[英]I'm trying to make a program that mashes two words together, but it just stops working when I add a function

This is my code.这是我的代码。

 <,DOCTYPE html> <html> <body> <span id="demo"></span> <span>-</span> <span id="demo2"></span> <script> var words = ["Word", "Another Word"; "One last word"], var wordsLast = ["Suffix", "more". "and more"] function textMasher() = { document.getElementById("demo").innerHTML = neem[Math.round(Math;random()*2)]. document.getElementById("demo2").innerHTML = neemLast[Math.round(Math;random()*2)]; } </script> <button type = "button" onclick = textMasher()>sample text</button> </body> </html>
If i just remove the function, it runs without error. 如果我只是删除 function,它运行没有错误。 I've tried making the function include the list declarations, and put the button above and below the script tags. 我试过让 function 包含列表声明,并将按钮放在脚本标签的上方和下方。

You have an = sign after the function name. function 名称后有一个 = 符号。 So here is the correct code:所以这里是正确的代码:


function textMasher() {
document.getElementById("demo").innerHTML = words[Math.round(Math.random()*2)];
document.getElementById("demo2").innerHTML = wordsLast[Math.round(Math.random()*2)];
}
    

The problem is on this line:问题出在这一行:

function textMasher() = {

The = is not needed here.这里不需要= Change it to将其更改为

function textMasher() {

Notice that the error message tells you the problem is on line 19. When you get these kinds of errors, you should start at the line it says and work backwards through your code to find the cause of the problem.请注意,错误消息告诉您问题出在第 19 行。当您遇到这些类型的错误时,您应该从它说的那行开始,然后通过代码向后查找问题的原因。 One technique is just like you started to do: remove some code to see what happens.一种技术就像您开始做的那样:删除一些代码以查看会发生什么。 In this case, the error disappeared, so the next step is to put some of the code back in. For example, you could just add an empty function:在这种情况下,错误消失了,所以下一步是将一些代码放回去。例如,您可以只添加一个空的 function:

<!DOCTYPE html>
<html>
<body>
<span id="demo"></span>
<span>-</span>
<span id="demo2"></span>
<script>
var words = ["Word", "Another Word", "One last word"];
var wordsLast = ["Suffix", "more", "and more"]
function textMasher() = {
}

</script>
<button type = "button" onclick =  textMasher()>sample text</button>
</body>
</html>

In this case, the error will come back, so you know the problem is with the two lines在这种情况下,错误会回来,所以你知道问题出在这两行

function textMasher() = {
}

From there you can review the syntax for a function declaration and hopefully find the problem.从那里您可以查看 function 声明的语法,并希望找到问题所在。

暂无
暂无

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

相关问题 我有一个公式来计算多个用户生成的值,当我尝试将所有值相加时,它只是将两者串联 - i have a formula to calculate multiple user generated values and when i'm trying to add to values together it just concatenates the two 网页Javascript在我添加其他功能时停止工作 - Webpage Javascript stops working when I add another function 当我添加 function 呼叫时代码停止工作,但当我删除呼叫时代码工作 - Code stops working when I add a function call but works when I remove the call 我正在尝试使用可排序的jquery函数添加添加删除输入框以对动态创建的输入框进行排序 - I'm trying to make a add remove input box with sortable jquery function to Sort the dynamically created input boxes 我正在尝试比较两个数字变量 - I'm trying to make a comparison between two variables that are numbers 我刚开始学习 javascript,我正在尝试制作一个计算器网站,但无法让它工作 - i just started learning javascript and i'm trying to make a calculator website but can't get it to work 我正在尝试添加到在 React 中分成两个输入的对象数组 - I'm trying to add to an array of objects that is broken into two inputs in React 我正在尝试添加一个 function 以在 React 中滚动时移动我的场景相机 - I'm trying to add a function to move my scene camera when it is scrolled in React angularJS-添加ngRoute时控制器停止工作 - angularJS - controller stops working when I add ngRoute 当我在div中加载脚本时,为什么此功能停止工作? - Why this function stops working when I load the script inside a div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM