简体   繁体   English

使用按钮隐藏/取消隐藏div?

[英]Hide/unhide div with button?

    <h1>Welcome! Chat now!</h1>

    <button id="button">Chat Now</button>
    <button id="buttontwo">Chat Categories</button>



    <div id="login" style="visibility:hidden">
    <button id="closelogin">Close</button>
    <input type="text" placeholder="Username"/>
    <p id="loginshiz">Pick a username</p>
    <button id="go">Go</button>
    </div>

When the chat now button is pressed, I want to make the div "login" appear, and when the "closelogin" button inside the div is pressed, I want to make the whole div hidden again. 当按下立即聊天按钮时,我要使div出现“登录”,而当按下div内部的“ closelogin”按钮时,我要使整个div再次隐藏。 Currently if no buttons are pressed the div should be at hidden state, cheers! 当前,如果未按任何按钮,则div应该处于隐藏状态,干杯!

Use jQuery. 使用jQuery。 No way to do it plain html/css. 没有办法做到纯HTML / CSS。

$('#button').click(function() {
    $('#login').css('visibility', 'visible');
});
$('#closelogin').click(function() {
    $('#login').css('visibility', 'hidden');
});

If you don't have jQuery included, use javascript: 如果您没有包括jQuery,请使用javascript:

document.getElementById('button').onclick = function() {
    document.getElementById('login').style.visibility = 'visible';
}
document.getElementById('closelogin').onclick = function() {
    document.getElementById('login').style.visibility = 'hidden';
}

Look at my example without using of JavaScript. 看我的示例,不使用JavaScript。

    <input type="checkbox" id="box"/>
    <label id="container" for="box">
        <div id="button">Chat Now</div>
        <div id="login">
            <label for="box" id="closelogin">Close</label>
            <input type="text" placeholder="Username"/>
            <p id="loginshiz">Pick a username</p>
            <button id="go">Go</button>
        </div>
    </label>

and css 和CSS

#box{display: none;}
#container #login{ display: none;}
#box:checked + #container #login{ display: block;}

Fiddle http://jsfiddle.net/LUdyb/1/ 小提琴http://jsfiddle.net/LUdyb/1/

Hope this help. 希望能有所帮助。

Using javascript with the help of the button id you can make the div to be hidden by changing the css property to visible. 在按钮ID的帮助下使用javascript,可以通过将css属性更改为visible来使div隐藏。 while using jquery you can use toggle,hide,show. 使用jQuery时,您可以使用切换,隐藏,显示。

There is no way you can do this in html/css 您无法在html / css中执行此操作

You can use Jquery 您可以使用Jquery

$('#button').click(function() {
    $('#login').css('visibility', 'visible');
});

to close 关闭

$('#closelogin').click(function() {
    $('#login').css('visibility', 'hidden');
});

you just need to change the ID that is #closelogin and the .css('visibility', 'hidden') 您只需要更改#closelogin.css('visibility', 'hidden')的ID

You need to include Jquery library like this in your head or bottom of your page to make it work. 您需要在页面的顶部或底部包含这样的Jquery库,以使其正常工作。

eg: 例如:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

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

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