admin管理员组文章数量:1334320
i'm sorta new to this. i have this so far:
<style type="text/css">
#show_hide{display:none;}
</style>
<div id="show_hide">
ok
</div>
<input type="text" onfocus="document.getElementById('show_hide').style.display='block';">
it works when i click the input box to show the div. prob is i need it to hide again when i click somewhere else or "unfocus" any help? thanks!
i'm sorta new to this. i have this so far:
<style type="text/css">
#show_hide{display:none;}
</style>
<div id="show_hide">
ok
</div>
<input type="text" onfocus="document.getElementById('show_hide').style.display='block';">
it works when i click the input box to show the div. prob is i need it to hide again when i click somewhere else or "unfocus" any help? thanks!
Share asked Aug 27, 2010 at 17:06 MikeMike 111 gold badge1 silver badge2 bronze badges3 Answers
Reset to default 3So, your issue is that your CSS sets the default state of the div (to display:none
), then your JavaScript changes the state onfocus to display: block
--but you don't have any code to revert the div back to the hidden state.
With plain JavaScript I believe you'll want the onblur
event (focus lost) to handle this:
<input type="text"
onfocus="document.getElementById('show_hide').style.display='block';"
onblur="document.getElementById('show_hide').style.display='none';">
Here it is in action, with your sample
have you tried leveraging onblur event http://javascript.gakaa./div-blur-4-0-5-.aspx
also this post might help How to blur the div element?
The event handler you are looking for is called onBlur (http://www.w3schools./jsref/event_onblur.asp).
本文标签: javascripthow to show and hide a div using onfocusStack Overflow
版权声明:本文标题:javascript - how to show and hide a div using onfocus - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742265954a2443394.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论