简体   繁体   English

用打开它的相同按钮隐藏文本

[英]Hide text with the same button it opened it

how can I hide the text opened while clicking on a button by clicking on the same button ? 如何通过单击同一按钮来隐藏在单击按钮时打开的文本? In other words, the same button should show or hide a text when you click on it. 换句话说,单击同一按钮应显示或隐藏文本。

Here's my code that shows a text : 这是显示文本的代码:

<h3> 
    <button class="button" onclick="myFunction()" ><img src="infoicon.png" height="30"></button>
                    <p id="infos"></p>

                    <script>
                        function myFunction() {
                            document.getElementById("infos").innerHTML = "blablabla";
                    }
                    </script>
</h3>

Define a variable to save the button state 定义变量以保存按钮状态

 var clicked = 0; function myFunction() { if(clicked == 0 ) { document.getElementById("infos").innerHTML = "blablabla"; clicked = 1; } else { document.getElementById("infos").innerHTML = ""; clicked = 0; } } 
 <h3> <button class="button" onclick="myFunction()" ><img src="infoicon.png" height="30"></button> <p id="infos"></p> </h3> 

Try this out, it worked for me: 试试看,它对我有用:

 function showHide() { var demo = document.getElementById('demo'); if (demo.innerHTML === "") { demo.innerHTML = "bla"; } else { demo.innerHTML = ""; } } 
 <p id="demo"></p> <button onclick="showHide()">Show Hide</button> 

Quite simple, using jQuery— 使用jQuery非常简单

$( "#button" ).click(function() {     
    $('#text-to-toggle').toggle(200);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM