jquery解决onmouseover和onmouseout合用的bug问题

时间:2023-03-09 19:08:24
jquery解决onmouseover和onmouseout合用的bug问题

经常会遇到鼠标放到一个元素上显示另外一个元素,这两个元素是父子关系,比如在A上绑定mouseover和mouseout事件来显示或隐藏B元素,A元素包含B元素,当鼠标移到B元素后浏览器认为你移开了A,所以就隐藏了B,下边这段代码解决这个问题,

var price_tip_pop = null;
$('div.v-price-tips').mouseout(function(){
clearTimeout(price_tip_pop);
price_tip_pop=setTimeout(function(){
$('#price_tip').hide();
},400);
}).mouseover(function(){
if(price_tip_pop!=null){
clearTimeout(price_tip_pop);
price_tip_pop=null;
}
$('#price_tip').show();
});