[英]Calling a JavaScript function when a button is clicked in HTML
I wrote a simple JavaScript code that allows people to enter a number and it should return either FizzBuzz, Fizz, Buzz or just the number if it did not meet the conditions of the FizzBuzz game. 我写了一个简单的JavaScript代码,允许人们输入数字,如果不满足FizzBuzz游戏的条件,它应该返回FizzBuzz,Fizz,Buzz或仅返回数字。 I then tried to create a button in HTML with the following code:
然后,我尝试使用以下代码在HTML中创建按钮:
<button onclick="fBCalc(prompt("Enter a number!"))">FizzBuzz</button>
I have also tried this: 我也尝试过这个:
<input type="button" name="FizzBuzz" value="FizzBuzz" onClick="fBCalc(prompt("Enter a number!"))" />
Both methods did not work. 两种方法均无效。 I have searched Google for an answer but I have tried them and nothing happens when I click the button.
我已经在Google上搜索了答案,但是我尝试了它们,但是当我单击按钮时没有任何反应。 I made sure that the function was called correctly.
我确保该函数被正确调用。 This works without the button so I do not know why it shouldn't work with a button.
这在没有按钮的情况下有效,因此我不知道为什么它不应该与按钮一起工作。 Unless a button is unable to prompt oo Does anyone know what is wrong?
除非按钮无法提示oo有人知道出什么问题了吗?
Here is a pastebin to my full HTML code: http://pastebin.com/YPGdbTVQ 这是我完整的HTML代码的pastebin: http : //pastebin.com/YPGdbTVQ
You haven't pasted your code properly here, but I did spy what I suspect is your issue on your Pastebin: 您尚未在此处正确粘贴代码,但我确实监视了我怀疑是您的Pastebin上的问题:
<button onclick="fBCalc(prompt("Enter a number!"))">FizzBuzz</button>
You can't put quotes inside of quotes the way you have here, because the browser can't figure out which ones do what. 您无法像在此处那样将引号放入引号内,因为浏览器无法确定哪些引号在做什么。 Try swapping the inner quotes with single-quotes:
尝试将内引号与单引号交换:
<button onclick="fBCalc(prompt('Enter a number!'))">FizzBuzz</button>
Coincidentally, good luck with your grades! 巧合的是,祝您成绩好! ;)
;)
您不能将双引号放在双引号内,因此使代码像这样
<button onclick="fBCalc(prompt('Enter a number!'))">FizzBuzz</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.