单击页面隐藏div的两个想法

想了一个

第一个想法分为两个步骤。

第一步:将事件处理程序绑定到文档的单击事件以隐藏div

第二步:点击事件绑定事件处理程序在div防止事件冒泡和防止起泡的文件,和onclick方法调用文件隐藏DIV。
复制代码代码如下所示:

函数里面(e){
如果(e.stoppropagation)
e.stoppropagation();
其他的
e.cancelbubble =真;
}

$(document)。Bind(听到咔哒声,函数()){
$(# test)。Css(显示器,不关);
});

$(# test)。Bind(听到咔哒声,功能(e){ {)
里面(E);
});


所以当点击非DIV区域,onclick方法直接或多层的泡沫会调用文件,隐藏的div,并点击div或其元素,事件总是泡DIV本身,这段时间将停止事件继续冒泡,而不是调用OnClick方法文献div隐藏,从而完成美国的需求。

思想的两

We mentioned before, when the trigger an event on the DOM will generate a event event object, this object contains all events and related information, including information related to generate event elements, event type, thought of div in the click event processing program is passed into the parameters of the event object.There are several different ways to access the event objects in IE, depending on the method of specifying the event handler.When the event handler is added directly to the DOM element, the event object exists as an attribute of the window object.

Event object contains an important attribute: target (W3C) /srcElement (IE), which identifies the original elements of triggering events, and the idea two is to use this property.We can directly handle the click event binding event handler for document, and read whether the event source is id==test's div element or its sub element in the event handler. 如果是这样的话,还不做手术,如果不是,它将隐藏的div
复制代码代码如下所示:

$(document)。Bind(听到咔哒声,功能(e){ {)
VaR E = e window.event / / | |;浏览器兼容性
var elem = e.target e.srcelement | |;
而(元){ / /环防止与节点,在div元素点击
如果(elem.id元。ID = =测试){
返回;
}
elem.parentnode elem =;
}

$(# test)。Css(显示器,不关'); / /点击div或它的子元素
});


这样,当在任何地方单击页面时,它都会冒泡以记录所有级别的单击事件。事件处理程序将确定事件源是否是id =测试的div或其子元素。如果是方法返回,否则,隐藏div也可以满足我们的需要。