单击按钮时如何隐藏/显示div?

Jul*_*lia 32 html button show hide

我有一个div包含注册向导,我需要div在单击按钮时隐藏/显示它.我怎样才能做到这一点?

下面我给你看一下代码.

谢谢 :)

  <div id="wizard" class="swMain">
    <ul>
      <li><a href="#step-1">
            <label class="stepNumber">1</label>
        </a></li>
      <li><a href="#step-2">
            <label class="stepNumber">2</label>
        </a></li>
      <li><a href="#step-3">
            <label class="stepNumber">3</label>
         </a></li>
      <li><a href="#step-4">
            <label class="stepNumber">4</label>
        </a></li>
    </ul>
    <div id="step-1"> 
        <h2 class="StepTitle">Perfil</h2>
        <table cellspacing="3" cellpadding="3" align="center">
            <tr>
                  <td align="center" colspan="3">&nbsp;</td>
            </tr>        
            <tr>
                  <td align="right">Username :</td>
                  <td align="left">
                    <input type="text" id="username" name="username" value="" class="txtBox">
                  </td>
                  <td align="left"><span id="msg_username"></span>&nbsp;</td>
            </tr>
            <tr>
                  <td align="right">Password :</td>
                  <td align="left">
                    <input type="password" id="password" name="password" value="" class="txtBox">
                  </td>
                  <td align="left"><span id="msg_password"></span>&nbsp;</td>
            </tr>                                          
       </table>               
    </div>
Run Code Online (Sandbox Code Playgroud)

小智 49

使用JQuery.您需要在按钮上设置单击事件,以切换向导div的可见性.

$('#btn').click(function() {
    $('#wizard').toggle();
});
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅JQuery网站.

这也可以在没有JQuery的情况下完成.仅使用标准JavaScript:

<script type="text/javascript">
   function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
   }
</script>
Run Code Online (Sandbox Code Playgroud)

然后添加onclick="toggle_visibility('id_of_element_to_toggle');"到用于显示和隐藏div的按钮.

  • 只需注意OP:当你不希望#wizard div为其他元素释放空间时,使用`visibility:hidden/visible;`.使用`display:none/block;`如果你想隐藏#wizard div,其大小,边距和填充属性在隐藏时等于0. (3认同)

xpt*_*xpt 16

这有效:

     function showhide(id) {
       	var e = document.getElementById(id);
       	e.style.display = (e.style.display == 'block') ? 'none' : 'block';
     }
Run Code Online (Sandbox Code Playgroud)
    <!DOCTYPE html>
    <html>   
    <body>
    
    	<a href="javascript:showhide('uniquename')">
    		Click to show/hide.
    	</a>
    
    	<div id="uniquename" style="display:none;">
    		<p>Content goes here.</p>
    	</div>
    
    </body>
    </html>
Run Code Online (Sandbox Code Playgroud)


Jar*_*ski 6

仅使用HTML/CSS无法做到这一点.你需要在这里使用javascript.在jQuery中它将是:

$('#button').click(function(e){
    e.preventDefault(); //to prevent standard click event
    $('#wizard').toggle();
});
Run Code Online (Sandbox Code Playgroud)


小智 5

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Show and hide div with JavaScript</title>
<script>

    var button_beg = '<button id="button" onclick="showhide()">', button_end = '</button>';
    var show_button = 'Show', hide_button = 'Hide';
    function showhide() {
        var div = document.getElementById( "hide_show" );
        var showhide = document.getElementById( "showhide" );
        if ( div.style.display !== "none" ) {
            div.style.display = "none";
            button = show_button;
            showhide.innerHTML = button_beg + button + button_end;
        } else {
            div.style.display = "block";
            button = hide_button;
            showhide.innerHTML = button_beg + button + button_end;
        }
    }
    function setup_button( status ) {
        if ( status == 'show' ) {
            button = hide_button;
        } else {
            button = show_button;
        }
        var showhide = document.getElementById( "showhide" );
        showhide.innerHTML = button_beg + button + button_end;
    }
    window.onload = function () {
        setup_button( 'hide' );
        showhide(); // if setup_button is set to 'show' comment this line
    }
</script>
</head>
<body>
    <div id="showhide"></div>
    <div id="hide_show">
        <p>This div will be show and hide on button click</p>
    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)