开发者

How can I have a mouseover/in one off check?

How can I test if a mouse is within a given div? I know how to use events to do something when it occurs but is it possible to check it 开发者_如何学JAVAon pageload only?

What I want to do is smething like:

    if(mouse is in specified div) {
        check = true;
    } else {
        check = false;
    }

Thanks, Denis


This will work on the first hover over the div and then never again. Do you want to check only if the mouse is over the div immediately on page load or just the first time the mouse hovers over it? If you want the former, it might be a little trickier, but check out this SO post to see how to do it.

$('div').hover(
  function() {
     alert('in the div');
  },
  function() {
     $(this).unbind();
  }
);


You can put the event at the top of your site's page like so (which would require the .js files to load in the header):

$('element').mouseover( function() {
    function_to_execute(); 
});

And then, at the bottom of your page, just before the end </body> have it say:

<script type="text/javascript>
    $('element').unbind();
</script>

To keep it from activating after page load. If that's what you wanted. I wasn't entirely clear based on your question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜