javascript 事件的问题 <input type='text' onclick='openDialog()'> 我有个输入框,当我点击输入框时,就会在输入框的下面跳出一个div,我希望当我再点击输入框意外的地方(以外的地方不包括div的地方)时,这个div消失。请问该用什么事件 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你可以这样实现:1>给div绑定一个click事件,不执行任何操作(记得 return FALSE ,防止事件冒泡)2>在给document绑定一个click事件,影藏div3>openDialog()函数里面也要return false; <html><head> <title>zjpdemo </title> <script> function fn(flag) { if (flag == 0) { document.getElementById("openDiv").style.display = "block"; } else { document.getElementById("openDiv").style.display = "none"; } }; </script></head><body> <input type="text" onblur="fn(1)" onfocus="fn(0)" style="width:150"> <div id="openDiv" style="display: none; width: 150; height: 150; background-color: Red"> 111111111111 </div></body></html> 二楼的谢谢回帖!但是没有达到我要的效果当我点击div时,div消失了,我要的效果是点击input和div时不消失,点击其它的地方才消失。因为我点击,div出来后,要操作div的内容。使用不能让它消失一楼的是个好办法。但是有些复杂! 可以根据事件源target来判断是否消失 <html><head> <title>zjpdemo </title> <script> function fn(flag) { if (flag == 0) { document.getElementById("openDiv").style.display = "block"; } else { document.getElementById("openDiv").style.display = "none"; } }; </script></head><body> <input type="text" onFocus="fn(0)" style="width:150"> <div id="openDiv" style="display: none; width: 150; height: 150; background-color: Red" onBlur="fn(1)" > 111111111111 </div></body></html>修改二楼的 点击文本框的时候DIV出现,然后点击除文本框和DIV以外的区域,DIV消失(但是必须要先点击一下DIV,才能实现这个效果!一楼的确实是个好办法,但是比较麻烦!) document.body.onclick=function(){document.getElementById("openDiv").style.display = "none";} 再补充一下,你可以在DIV上加一个关闭按钮 <input type="button" onFocus="fn(1)" value="关闭"><html><head> <title>zjpdemo </title> <script> function fn(flag) { if (flag == 0) { document.getElementById("openDiv").style.display = "block"; } else { document.getElementById("openDiv").style.display = "none"; } }; </script></head><body> <input type="text" onFocus="fn(0)" style="width:150"> <div id="openDiv" style="display: none; width: 150; height: 150; background-color: Red" onBlur="fn(1)" > 这里是要显示的信息,点击关闭按钮即可关闭<input type="button" onFocus="fn(1)" value="关闭"> </div></body></html> 求助jsp中添加打印方法不显示页眉页脚 js里面加onclick如何加? 大家请进 JS怎么捕捉函数发生呢 有没有人能实现这个具有挑战性的问题(事件+思想)(续) 如何验证表单? 在Javascript 中查找 加号(+),是什么格式? 怎样取出链接“<a href="http://pgcn.net">盘古</a>”中的盘古? 谁能告诉我这个函数的具体作用与意义?(得答案即结贴) 请问,我想在做一个当用户点击一个按钮时弹出一个输入框写密码。当用户输入时,它的输入框中显示"*"。应如何做啊?? 一道有难度的javascript题目 求判断IE7、IE8、及火狐的各个版本JS
<html>
<head>
<title>zjpdemo </title> <script>
function fn(flag) {
if (flag == 0) {
document.getElementById("openDiv").style.display = "block";
} else {
document.getElementById("openDiv").style.display = "none"; }
};
</script></head>
<body>
<input type="text" onblur="fn(1)" onfocus="fn(0)" style="width:150">
<div id="openDiv" style="display: none; width: 150; height: 150; background-color: Red">
111111111111
</div>
</body>
</html>
当我点击div时,div消失了,我要的效果是点击input和div时不消失,点击其它的地方才消失。
因为我点击,div出来后,要操作div的内容。使用不能让它消失一楼的是个好办法。但是有些复杂!
<head>
<title>zjpdemo </title> <script>
function fn(flag) {
if (flag == 0) {
document.getElementById("openDiv").style.display = "block";
} else {
document.getElementById("openDiv").style.display = "none"; }
};
</script></head>
<body>
<input type="text" onFocus="fn(0)" style="width:150">
<div id="openDiv" style="display: none; width: 150; height: 150; background-color: Red" onBlur="fn(1)" >
111111111111
</div>
</body>
</html>
修改二楼的 点击文本框的时候DIV出现,然后点击除文本框和DIV以外的区域,DIV消失(但是必须要先点击一下DIV,才能实现这个效果!一楼的确实是个好办法,但是比较麻烦!)
<html>
<head>
<title>zjpdemo </title> <script>
function fn(flag) {
if (flag == 0) {
document.getElementById("openDiv").style.display = "block";
} else {
document.getElementById("openDiv").style.display = "none"; }
};
</script></head>
<body>
<input type="text" onFocus="fn(0)" style="width:150">
<div id="openDiv" style="display: none; width: 150; height: 150; background-color: Red" onBlur="fn(1)" >
这里是要显示的信息,点击关闭按钮即可关闭<input type="button" onFocus="fn(1)" value="关闭">
</div>
</body>
</html>