【问题标题】:select element onmouseleave event in safari在 safari 中选择元素 onmouseleave 事件
【发布时间】:2016-08-28 07:37:26
【问题描述】:
我想用下面的代码捕捉鼠标离开选择框。
在 Chrome 中这很好用,但在 Safari 中,当从框移动到里面的选项时也会触发该事件。
如何让 onmouseleave 事件仅在离开选择元素时触发?
谢谢!
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>
【问题讨论】:
标签:
javascript
html
safari
【解决方案1】:
似乎 onmouseleave 在 safari 中没有得到很好的支持,所以我最终改用了 onmouseout:
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>