javascript - Properly keeping track of which (nested) div has mouse-over - Stack Overflow

I have a (deeply) nested structure of <div>s. With Javascript, I'd like to keep track of whi

I have a (deeply) nested structure of <div>s. With Javascript, I'd like to keep track of which element the mouse is currently over (i.e., 'has focus'), where more deeply nested elements have priority:

I've tried binations of mouseover, mouseout, mouseenter, mouseleave and mousemove, but there seems to be no simple solution that gives me the expected behavior. I am able to receive mouse-events on the right divs, but often these events are subsequently received by higher level divs, which would then undeservedly take focus.

For example, in the transition above from a to c, the event might then be received by b last, unintentionally giving focus to b rather than c. Or the transition from c to b might not be registered at all for some reason.

I don't understand the underlying mechanism of mouse-event propagation well enough to e up with a reliable solution. It seems like it should be such a simple thing, but I can't figure it out.

I've been able to get it to work as follows: when a div receives a mouseenter/over event, flag it and search the entire DOM subtree. If any other flags are found deeper down, relinquish focus. But this is rather crude, and I can't help but think there must be an easier way.

Edit: Solution

The use of mouseover and mouseout, bined with a call to stopPropagation() seems to work very well. Here is a JSFiddle to show the working solution.

I have a (deeply) nested structure of <div>s. With Javascript, I'd like to keep track of which element the mouse is currently over (i.e., 'has focus'), where more deeply nested elements have priority:

I've tried binations of mouseover, mouseout, mouseenter, mouseleave and mousemove, but there seems to be no simple solution that gives me the expected behavior. I am able to receive mouse-events on the right divs, but often these events are subsequently received by higher level divs, which would then undeservedly take focus.

For example, in the transition above from a to c, the event might then be received by b last, unintentionally giving focus to b rather than c. Or the transition from c to b might not be registered at all for some reason.

I don't understand the underlying mechanism of mouse-event propagation well enough to e up with a reliable solution. It seems like it should be such a simple thing, but I can't figure it out.

I've been able to get it to work as follows: when a div receives a mouseenter/over event, flag it and search the entire DOM subtree. If any other flags are found deeper down, relinquish focus. But this is rather crude, and I can't help but think there must be an easier way.

Edit: Solution

The use of mouseover and mouseout, bined with a call to stopPropagation() seems to work very well. Here is a JSFiddle to show the working solution.

Share Improve this question edited May 8, 2014 at 13:02 mhelvens asked May 8, 2014 at 12:25 mhelvensmhelvens 4,3334 gold badges36 silver badges57 bronze badges 1
  • Thanks for this. I was struggling with a similar problem. Your solution helped. – Sidd Commented May 28, 2020 at 12:38
Add a ment  | 

2 Answers 2

Reset to default 4

You can use the event stopPropagation() method:

https://developer.mozilla/en-US/docs/Web/API/event.stopPropagation

If using jQuery, try calling stopPropagation() on the event passed as the first parameter

http://api.jquery./event.stoppropagation/

$( "p" ).on('mouseover', function( event ) {
  event.stopPropagation();
  // Do something
});

Also, you can check which element is the target, as in event.target.

For non-IE browsers & IE >= 9 use,

    evt.stopPropagation();

For Legacy IE browsers(IE<9) browsers use,

    evt.cancelBubble = true;

This will prevent bubbling of events to parent elements.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744750311a4591554.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信