页面上有一对Div标签,我希望鼠标在这标签所处位置之外的任何地方点击之后该标签隐藏,但在该标签本身上面点击时不会使它隐藏。
我自己写的两句代码(如下)不能用,因为在该标签上面点击时也会让它隐藏起来<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body >
<form runat ="server" id="form1">
<div id="div1">aaaaaaa</div>
</form>
</body>
<script language ="javascript" >
function a()
{
var div1=document.getElementById('div1');
div1.style.visibility='hidden'
}
document.onmousedown=a;
</script>
</html>
我自己写的两句代码(如下)不能用,因为在该标签上面点击时也会让它隐藏起来<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body >
<form runat ="server" id="form1">
<div id="div1">aaaaaaa</div>
</form>
</body>
<script language ="javascript" >
function a()
{
var div1=document.getElementById('div1');
div1.style.visibility='hidden'
}
document.onmousedown=a;
</script>
</html>
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body > <form runat ="server" id="form1"> <div id="div1">aaaaaaa</div> </form> </body> <script language ="javascript" > function a() { var div1=document.getElementById('div1'); div1.style.display='none' } document.onmousedown=a; </script> </html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body >
<form runat ="server" id="form1">
<div id="div1">aaaaaaa</div>
</form>
</body>
<script language ="javascript" >
function a()
{
var div1=document.getElementById('div1');
div1.style.visibility='hidden'
}
document.onmousedown=a;
</script>
</html>
------------
为什么你的不能用?因为你的js处理代码并没有和任何事件绑定即没有触发源头,如何有作用了?
你的脚本没有执行。。
执行了的,一点击“aaaaaaa”就消失了。你的机上没执行???
----------
楼主的问题是要在标签外点击就隐藏,你的? --!
{
if(event.srcElement.id == divID)
document.getElementById('divID').style.display = "";
else
document.getElementById('divID').style.display = "none";
}
鼠标后一定会执行方法a的。其实写成<body onmousedown="a()">也一样。但问题的关键是我写的这个方法a
不对,请知道的哥们给个对的代码。谢 谢
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page </title>
</head>
<body >
<form runat ="server" id="form1">
<div id="div1">aaaaaaa </div>
</form>
</body>
<script language ="javascript" >
function a()
{
var div1=document.getElementById('div1');
div1.style.visibility='hidden'
}
document.onmousedown=a;
</script>
</html>
------------
为什么你的不能用?因为你的js处理代码并没有和任何事件绑定即没有触发源头,如何有作用了?
=----------------
汗,收回这句话,没看到下面的onmousedown,我的也改下!window.onmousedown= function()
{
if(event.srcElement.id == divID)
document.getElementById('divID').style.display = "";
else
document.getElementById('divID').style.display = "none";
}
但我想应该是这个思路吧。可能是event挑浏览器的问题,我再看看,谢了你的思路
document.getElementById('divID').style.display = "";
else
document.getElementById('divID').style.display = "none";
------------
ie下面是有srcElement,不过firefox下面是target