oEvent.cancelBubble = true;
点击页面隐藏(模仿下拉列表)
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1 {
width: 400px;
height: 300px;
background: #CCC;
display: none;
}
</style>
<script>
window.onload = function () {
var oBtn = document.getElementById('btn1');
var oDiv = document.getElementById('div1');
oBtn.onclick = function (ev) {
oDiv.style.display = 'block';
var oEvent = ev || event;
oEvent.cancelBubble = true; // 取消事件冒泡(否则会触发页面点击操作,导致隐藏)
};
document.onclick = function () { // 点击页面隐藏
oDiv.style.display = 'none';
};
};
</script>
</head>
<body>
<input id="btn1" type="button" value="显示" />
<div id="div1">
</div>
</body>
</html>