开发者

Java script function to hide a <span>?

开发者 https://www.devze.com 2023-03-29 04:41 出处:网络
Hi i have the following code: <a onclick=\"show_login()\">Login <span id=\"login\" style=\"visibility: hidden\">

Hi i have the following code:

<a onclick="show_login()">Login
<span id="login" style="visibility: hidden">
<form method="post" action="login.php"> 
<label for="username">Username:</label>
<input type="text" name="username" /></a>
<label for="password">Password:</label>
<input type="password" name="password" />

<input 开发者_运维知识库type="submit" name="login" value="Login" />
</form>
</span>

I want to know how to hide/show that span using the function name "show_login". How would i go about doing this?


You probably shouldn't be using a <span> for that, <div> would serve you better. And visibility probably isn't what you want either, visible: hidden:

hidden
The generated box is invisible (fully transparent, nothing is drawn), but still affects layout. Furthermore, descendants of the element will be visible if they have 'visibility: visible'.

And you should close your <a> before the login form.

You probably want to use display: none and display: block. So something like this:

<a onclick="toggle_login()">Login</a>
<div id="login" style="display: none">
    <form method="post" action="login.php"> 
        <label for="username">Username:</label>
        <input type="text" name="username" /></a>
        <label for="password">Password:</label>
        <input type="password" name="password" />
        <input type="submit" name="login" value="Login" />
    </form>
</div>

And:

function toggle_login() {
    var div = document.getElementById('login');
    div.style.display = div.style.display == 'none' ? 'block' : 'none';
    return false;
}

Live example: http://jsfiddle.net/ambiguous/6ngnU/1/


Set the style to display: none to start with and use the jquery function show to make it appear when you want it.

http://api.jquery.com/show/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号