日期:2014-05-16 浏览次数:20479 次
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8" /> <title>test</title> <style> #div1{width:100px;height:100px;border:1px solid black;display:block;} #div2{width:100px;height:100px;border:1px solid black;display:none;} </style> </head> <body> <div id='div1'></div> <div id='div2'></div> <script src="jquery.js"></script> <script> (function(){ var $div1 = $("#div1"), $div2 = $("#div2"); $div1.hover(function(e){ $div2.show(); }, function(e){ console.log(isOrContains($div2, e.relatedTarget)); if(!isOrContains($div2, e.relatedTarget)){ $div2.hide(); } }); $div2.bind("mouseleave", function(e){ console.log(isOrContains($div1, e.relatedTarget)); if(!isOrContains($div1, e.relatedTarget)){ $(this).hide(); } }); function isOrContains(elem1, elem2){ var $elem1 = elem1.jQuery ? elem1 : $(elem1), $elem2 = elem2.jQuery ? elem2 : $(elem2); return ($elem1.is($elem2) || $elem1.has($elem2).length !== 0) ? true : false; } })(); </script> </body> </html>