javascript 单击按钮时如何在div中显示值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18951250/
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
how to display a value in div when click a button?
提问by lelouchliana
i'm still new to this. . i want the value to display in a div that i already defined in css when i click a button. but the only thing that come out is the button. i just can't get the right result that i wanthere's what i've done.
我还是新手。. 当我单击按钮时,我希望该值显示在我已经在 css 中定义的 div 中。但唯一出来的是按钮。我只是无法得到我想要的正确结果,这就是我所做的。
<?php
var $val=001;
function increase(){
echo "<div id='display' >$val++</div>" ;
}
?>
<body>
<div id="display">
<button type="button" onclick="increse()">click me!!</button>
</div>
</body>
---------------------my #display in css-------------
.display{
height: 200px;
width: 500px;
background-color:#CECECE;
}
please help. or maybe refer me to something useful. thank you :)
请帮忙。或者给我推荐一些有用的东西。谢谢 :)
回答by
<body>
<div id="display">
<button type="button" onclick="increase()">click me!!</button>
</div>
</body>
you should use javascript with this `
你应该使用带有这个`的javascript
function increase()
{
var val=001;
document.getElementById('display').innerHTML=val++;
}
</script>`
if you want to do this in php:
如果您想在 php 中执行此操作:
<body>
<div id="display">
<button type="button" onclick="<?php increase(); ?>">click me!!</button>
</div>
</body>
but this will not give the wanted results as this will append another div with same id display
但这不会给出想要的结果,因为这会附加另一个具有相同 id 显示的 div
回答by Vaibs_Cool
HTML
HTML
<div class="likes">13</div>
<button class="btn">Increment</button>
jQuery
jQuery
$('.btn').click(function() {
var num = parseInt($('.likes').text());
$('.likes').text(num+1);
});
Now you can modify it according to your needs
现在你可以根据你的需要修改它