Html Javascript将变量写入div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19703625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript write variable to div
提问by Micha Sprengers
I'm trying to write the variable "current" to the div "bank" using the following code when the user clicks on the image:
document.getElementById('bank').innerHTML = current
当用户单击图像时,我正在尝试使用以下代码将变量“current”写入 div“bank”:
document.getElementById('bank').innerHTML = current
According to my research, this should work, but it doesn't. Here's my code:
根据我的研究,这应该有效,但事实并非如此。这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="css/default.css">
<script>
var current = 0;
var totalThisGame = 0;
function onButtonClick
{
current = current +1;
totalThisGame = totalThisGame +1;
document.getElementById('bank').innerHTML = current;
}
</script>
</head>
<body>
<div id="left">
<div id="bank">
</div>
<img id="image" src="img/image.png" alt="image" onclick=onButtonClick();>
</div>
<div id="middle">
</div>
<div id="right">
</div>
</body>
</html>
回答by MAQU
you miss the () behind your function
你错过了函数后面的 ()
function onButtonClick()
{
current = current +1;
totalThisGame = totalThisGame +1;
document.getElementById('bank').innerHTML = current;
}
your code is working fine for me
你的代码对我来说很好用
回答by Micha Sprengers
error
错误
change function onButtonClick -> function onButtonClick()
<img id="image" src="img/image.png" alt="image" onclick=onButtonClick();> to
<img id="image" src="img/image.png" alt="image" onclick="onButtonClick();">
回答by DavidB
Try:
尝试:
<img id="image" src="img/image.png" alt="image" onclick="onButtonClick();">
回答by TN888
Your function is badly defined. It should be function onButtonClick()
instead of function onButtonClick
. Then use that HTML code : <img id="image" src="img/image.png" alt="image" onclick="onButtonClick();">
您的功能定义不正确。它应该function onButtonClick()
代替function onButtonClick
. 然后使用该 HTML 代码:<img id="image" src="img/image.png" alt="image" onclick="onButtonClick();">
回答by BhushanK
you forgot the function parenthesis
你忘了函数括号
function onButtonClick()
{
current = current +1;
totalThisGame = totalThisGame +1;
document.getElementById('bank').innerHTML = current;
}
回答by Govan
Bank has to be in double quotes: document.getElementById("bank").innerHTML = current;
银行必须是双引号: document.getElementById("bank").innerHTML = current;
回答by Lalbhusan Yadav
Always use any of the functions on any event in html within double quotes.Try this
始终在双引号内的 html 中的任何事件上使用任何函数。试试这个
回答by EricG
You should read the console message and you'd notice that your function is odd. Note parenthesis.
您应该阅读控制台消息,您会注意到您的函数很奇怪。注意括号。
function onButtonClick
{
becomes
变成
function onButtonClick()
{
回答by Shreedhar
change
改变
function onButtonClick to function onButtonClick()
<img id="image" src="img/image.png" alt="image" onclick=onButtonClick();>
<img id="image" src="img/image.png" alt="image" onclick=onButtonClick();>
to
到
<img id="image" src="img/image.png" alt="image" onclick="onButtonClick();">
it would be better if you put your javascript before the body closing tag or use window.onload method
如果您将 javascript 放在正文结束标记之前或使用 window.onload 方法会更好