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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:23:56  来源:igfitidea点击:

Hide button after clicked

javascripthtml

提问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

You can use jQuery hide

您可以使用 jQuery隐藏

$("#myDiv").hide() // to hide the div

and showlike

显示

$("#myDiv").show() // to show the div

Or toggleto toggle the visibility of dom elements

切换以切换 dom 元素的可见性

$("#myDiv").toggle() // to toggle the visibility

回答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">