javascript 单击后隐藏按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29963088/
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
Hide button after clicked
提问by Mo-Alanteh
I am trying to hide a button (not inside form tags) after it has been clicked. Once the form is shown, there is no use for the button. So i would like to hide it after clicked
我试图在单击后隐藏按钮(不在表单标签内)。一旦显示了表单,按钮就没有用了。所以我想在点击后隐藏它
Here's the existing code.
这是现有的代码。
<script type="text/javascript">
$(function(){
var button = document.getElementById("info");
var myDiv = document.getElementById("myDiv");
function show() {
myDiv.style.visibility = "visible";
}
function hide() {
myDiv.style.visibility = "hidden";
}
function toggle() {
if (myDiv.style.visibility === "hidden") {
show();
} else {
hide();
}
}
hide();
button.addEventListener("click", toggle, false);
});
</script>
<input id="info" type="button" value="Имате Въпрос?" class="switchbuton">
回答by Robin
回答by uzay95
You can check the result here:
您可以在此处查看结果:
http://jsfiddle.net/jsfiddleCem/33axo20f/2/
http://jsfiddle.net/jsfiddleCem/33axo20f/2/
Code is:
代码是:
<style>
.showButon{
background:url('http://spacetelescope.github.io/understanding-json-schema/_static/pass.png');
background-repeat:repeat-y;
height:30px;
text-indent:20px;
}
</style>
<div id="myDiv">
<input id="info" type="button" value="Имате Въпрос?" class="showButon" />
</div>
(function(){
var button = document.getElementById("info");
var myDiv = document.getElementById("myDiv");
function toggle() {
if (myDiv.style.visibility === "hidden") {
myDiv.style.visibility = "visible";
} else {
myDiv.style.visibility = "hidden";
}
}
button.addEventListener("click", toggle, false);
})()
回答by Kartic
Why don't you use:
为什么不使用:
<script type="text/javascript">
$(function(){
$('#info').click(function() {
$(this).hide();
});
});
</script>
<input id="info" type="button" value="Имате Въпрос?" class="switchbuton">