关于Div显示

时间:2010-06-11 05:56:16

标签: javascript html

当我点击按钮时,显示一个div ..每当我点击身体时,该div将被隐藏。

我们如何实现这一功能..我写一个代码如下。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript">
function HideSubMenus(e)
{
 var targ;
 if (!e) var e = window.event;
 if (e.target) targ = e.target;
 else if (e.srcElement) targ = e.srcElement;
 if (targ.nodeType == 3) // defeat Safari bug
  targ = targ.parentNode;
 for (var i=0; i!=5; i++) {
  if (!targ) {
   break;
  } else if (targ.className=="divclass") {
   return;
  }
  targ = targ.parentNode;
 }

 if(document.getElementById("showdivid"))
 {
  document.getElementById("showdivid").style.display='';
 }
}
</script>
</head>

<body onclick="HideSubMenus(event);">
<input type="button" name="button" value="Click ME" onclick="document.getElementById('showdivid').style.display='';" />
<div id="showdivid" class="divclass" style="display:none;">
TeSt DIV
</div>
</body>
</html>

问候 拉吉

1 个答案:

答案 0 :(得分:1)

希望这有帮助。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript">
function HideSubMenus(e)
{
 var targ;
 if (!e) var e = window.event;
 if (e.target) targ = e.target;
 else if (e.srcElement) targ = e.srcElement;
 if (targ.nodeType == 3) // defeat Safari bug
  targ = targ.parentNode;
  if(targ.className==='btn'){
    document.getElementById('showdivid').style.display='';
    return;
  }else{
    document.getElementById('showdivid').style.display='none';
  }
}

</script>
</head>

<body onclick="HideSubMenus(event);">
<input type="button" name="button" class='btn' value="Click ME" />
<div id="showdivid" class="divclass" style="display:none;">
TeSt DIV
</div>
</body>
</html>