最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Keep checking condition instead of one - time checking in jquery js - Stack Overflow

programmeradmin1浏览0评论

What I would like to achieve is when the mouse is not hovered menu3, the system will keep checking whether the aboutMenu is hovered, if yes, alert 'h', and alert 'nh' otherwises. The problem is it only check one time when the mouse leave menu3, how to fix this problem? thanks.

$('#menu3').live('mouseout',function() {

$("#aboutMenu").hover(function() {
    $(this).data("hovered", true);
}, function() {
    $(this).data("hovered", false);
}); 

if ( $("#aboutMenu").data("hovered") ) {
    alert ('h');
} else {
    alert ('nh');
}
});

Updated:

Or the other way to do this is the system is keep checking whether menu3 or aboutMenu is hovered, if not , the hovered message is popup. However, this will only run one-time when the page is initialized , how to make it keep checking? thanks

$(document).ready(function() {
$("#aboutMenu,#menu3").hover(function() {
$(this).data("hovered", true);
}, function() {
    $(this).data("hovered", false);
}); 

if ( $("#aboutMenu,#menu3").data("hovered") ) 
alert ('hovered');
}); 

What I would like to achieve is when the mouse is not hovered menu3, the system will keep checking whether the aboutMenu is hovered, if yes, alert 'h', and alert 'nh' otherwises. The problem is it only check one time when the mouse leave menu3, how to fix this problem? thanks.

$('#menu3').live('mouseout',function() {

$("#aboutMenu").hover(function() {
    $(this).data("hovered", true);
}, function() {
    $(this).data("hovered", false);
}); 

if ( $("#aboutMenu").data("hovered") ) {
    alert ('h');
} else {
    alert ('nh');
}
});

Updated:

Or the other way to do this is the system is keep checking whether menu3 or aboutMenu is hovered, if not , the hovered message is popup. However, this will only run one-time when the page is initialized , how to make it keep checking? thanks

$(document).ready(function() {
$("#aboutMenu,#menu3").hover(function() {
$(this).data("hovered", true);
}, function() {
    $(this).data("hovered", false);
}); 

if ( $("#aboutMenu,#menu3").data("hovered") ) 
alert ('hovered');
}); 
Share edited Dec 22, 2012 at 10:28 user782104 asked Dec 22, 2012 at 10:21 user782104user782104 13.6k60 gold badges178 silver badges315 bronze badges 2
  • 3 why hover even in moueout ??? (it doesn't makes sense-imo) – NullPoiиteя Commented Dec 22, 2012 at 10:22
  • updated question, pls have a look – user782104 Commented Dec 22, 2012 at 10:29
Add a ment  | 

2 Answers 2

Reset to default 4
function checkHover() {
  if ($("#aboutMenu,#menu3").hasClass("hovered")) {
    alert ('hovered');
    //other stuff if hovered
  }
  else {
    //other stuff if not hovered
  }
}

$(document).ready(function() {
  $("#aboutMenu,#menu3").hover(function() {
    $(this).addClass("hovered");
  }, function() {
    $(this).removeClass("hovered");
  }); 

  setInterval(checkHover, 1000);
}); 

Would it be an idea to check the hoverstate periodically?

function chkhover(){
    if ($('#aboutMenu').is(':hover')){
        alert('#aboutMenu hoverstate is: hovered');
    }
    setTimeout(chkhover, 500);
}

$(document).ready(chkhover);

See this jsfiddle. It demonstrates a css-only solution too, by the way.

发布评论

评论列表(0)

  1. 暂无评论