[英]How to hide div by onclick using javascript?
I have used a javascript to show div by onclick but when i click outside div i want to hide the div. 我已经使用了一个javascript来通过onclick显示div但是当我点击div之外我想要隐藏div。 How to do it in javascript?
怎么在javascript中做到这一点? i'm using javascript code..
我正在使用javascript代码..
<a href="javascript:;" onClick="toggle('one');">
function toggle(one)
{
var o=document.getElementById(one);
o.style.display=(o.style.display=='none')?'block':'none';
}
HTML HTML
<a href="#" onclick="toggle(event, 'box');">show/hide</a>
Javascript 使用Javascript
// click on the div
function toggle( e, id ) {
var el = document.getElementById(id);
el.style.display = ( el.style.display == 'none' ) ? 'block' : 'none';
// save it for hiding
toggle.el = el;
// stop the event right here
if ( e.stopPropagation )
e.stopPropagation();
e.cancelBubble = true;
return false;
}
// click outside the div
document.onclick = function() {
if ( toggle.el ) {
toggle.el.style.display = 'none';
}
}
you can use blur() function when you clicked somewhere else 单击其他位置时可以使用blur()函数
$("#hidelink").click(function() {
$("#divtoHide").show();
});
$("#hidelink").blur(function() {
$("#divtoHide").hide();
});
使用jQuery,这将很简单:
$("button.hide").click(function(event){ $("div.hidethis").hide() });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.