Jquery点击div之外的地方隐藏当前div
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <title></title> <script type="text/javascript"> $(document).bind('click', function(e) { var e = e || window.event; //浏览器兼容性 var elem = e.target || e.srcElement; while (elem) { //循环判断至跟节点,防止点击的是div子元素 if (elem.id && elem.id == 'test') { return; } elem = elem.parentNode; } $('#test').css('display', 'none'); //点击的不是div或其子元素 }); </script> </head> <body> <div id="test" style="width: 300px; height: 300px; background-color: #CBC7BC;"> <table> <tr> <td>12</td> <td>23</td> </tr> <tr> <td>12</td> <td>23</td> </tr> </table> </div> </body> </html>
(本人微信号:Liberty-bcy)如果,你正在埋怨命运不眷顾,那请记住:命,是失败者的借口;运,是成功者的谦词。