有一个div,怎么才这个div以外的地方点一下这个div就隐藏了

来源:百度知道 编辑:UC知道 时间:2024/06/10 17:39:58
document.onclick 这样是所有的地方点一下都隐藏啊,我要div以外的地方点一下隐藏

document.onclick = function()
{
document.getElementById("div1").style.display = "none";
}

// 补充:
“document.onclick 这样是所有的地方点一下都隐藏啊,我要div以外的地方点一下隐藏”
禁用div的单击事件冒泡,即在div的onclick事件上里写:
window.event.cancelBubble = true;

<!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=gb2312" />
<title>无标题文档</title>
</head>

<body>
<script language="javascript">
function clos(){
var obj=document.getElemen