select element onmouseleave event in safari

跟風遠走 提交于 2020-01-25 22:01:35

问题


I want to catch the mouse leaving a select box with the code below.

In Chrome this works great, but in Safari the event fires also when moving from the box to the options inside.

How can I get the onmouseleave event to fire only when leaving the select element ?

thx!

var s=document.getElementById("myselect");
s.onmouseleave=function(e) { alert("mouseleave"); }
<select id="myselect" size=4>
  <option>first</option>
  <option>second</option>
  <option>third</option>
  <option>forth</option>
 </select>

回答1:


seems like onmouseleave is not well supported in safari so I ended up using onmouseout instead:

var s=document.getElementById("myselect");
s.onmouseout = (e => {
  let related=(e.relatedTarget ? e.relatedTarget.tagName : null);
  if (related!=='SELECT' && related!=='OPTION') alert("left select box");
});
<select id="myselect" size=4>
  <option>first</option>
  <option>second</option>
  <option>third</option>
  <option>forth</option>
 </select>


来源:https://stackoverflow.com/questions/39189017/select-element-onmouseleave-event-in-safari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!