Add an event listener to document
and use Node.contains()
to find whether the target of the event (which is the inner-most clicked element) is inside your specified element. It works even in IE5
var specifiedElement = document.getElementById('a');
//I'm using "click" but it works with any event
document.addEventListener('click', function(event) {
var isClickInside = specifiedElement.contains(event.target);
if (!isClickInside) {
//the click was outside the specifiedElement, do something
}
});
0 Komentar