자식 요소가 클릭되었는지 여부를 결정하기 위해 부울을 사용하는 것은 좋지 않습니까? 더 좋은 방법이 있습니까?자식 요소 인 경우 클릭 방지
참고 : jquery를 사용하고 싶지 않습니다. 아래
참조 코드 :
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
body{margin:0;}
#container{height:300px;background:red}
#box{width:500px;height:300px;background:blue;margin:auto}
</style>
</head>
<body>
<div id="container">
<div id="box"></div>
</div>
<script>
var hbWindow = window,
hbDocument = document,
hbBooleanIfIsOutside = new Boolean(),
hbIdBox = hbDocument.getElementById('box'),
hbIdContainer = hbDocument.getElementById('container');
hbWindow.onload = function() {
hbIdContainer.onclick = function() {
if(hbBooleanIfIsOutside) {
alert('you\'re outside!');
} else {
alert('you\'re inside!');
}
hbBooleanIfIsOutside = true;
}
hbIdBox.onclick = function() {
hbBooleanIfIsOutside = false;
}
}
</script>
</body>
</html>
추가 된 새로운 버전 : 나는 대신하여 addEventListener를 사용하고이 버전에서
.
var hbWindow = window,
hbDocument = document,
hbIdBox = hbDocument.getElementById('box'),
hbIdContainer = hbDocument.getElementById('container');
hbWindow.onload = function() {
function inOrOut(e){
if (!e) e = hbWindow.event;
if((e.target || e.srcElement).id == 'container') {
alert('you\'re outside!');
} else {
alert('you\'re inside!');
}
}
hbIdContainer.addEventListener('click', inOrOut, false);
}
가 왜'window'와'document' 별명입니까? – Mathletics
그냥 습관 ... – user1087110
습관, 물론, 왜 시작 했습니까? – Mathletics