IE8 在悬停选择框选项时中断
好的,我的以下代码可以在除 IE 之外的所有浏览器中正常运行。
$('input[title!=], select[title!=]').mouseenter(function(){
if ($(this).data('focused')!='y') {
$(this).data('t', this.title).data('focused', 'y');
this.title = '';
var pos = $(this).position();
$('body').append('<div id="toolTip" class="round-5 shadow-heavy"><img class="arrow" src="/images/bg/toolTip.png" alt="" />'+($(this).data('t'))+'</div>');
$('#toolTip').css('top',(pos.top+($(this).height()/2)-($('#toolTip').innerHeight()/2))+'px').css('left',(pos.left+($(this).innerWidth())+20)+'px');
}
}).mouseleave(function(){
if ($(this).data('focused')!='n') {
$(this).data('focused', 'n');
this.title = $(this).data('t');
$('#toolTip').remove();
}
}).focus(function(){if($(this).data('focused')!='y'){$(this).trigger('mouseenter');}}).blur(function(){if($(this).data('focused')!='n'){$(this).trigger('mouseleave');}});
现在,在 IE 中,如果您打开选择框并将鼠标移到其中一个选项上,该框就会关闭。造成这种情况的原因是 IE 显然没有将选项下拉框算作选择元素的一部分,因此它触发了 mouseleave 事件。
有谁知道解决这个问题吗?
Okay, so I have the following code that works fine in all browsers except IE..
$('input[title!=], select[title!=]').mouseenter(function(){
if ($(this).data('focused')!='y') {
$(this).data('t', this.title).data('focused', 'y');
this.title = '';
var pos = $(this).position();
$('body').append('<div id="toolTip" class="round-5 shadow-heavy"><img class="arrow" src="/images/bg/toolTip.png" alt="" />'+($(this).data('t'))+'</div>');
$('#toolTip').css('top',(pos.top+($(this).height()/2)-($('#toolTip').innerHeight()/2))+'px').css('left',(pos.left+($(this).innerWidth())+20)+'px');
}
}).mouseleave(function(){
if ($(this).data('focused')!='n') {
$(this).data('focused', 'n');
this.title = $(this).data('t');
$('#toolTip').remove();
}
}).focus(function(){if($(this).data('focused')!='y'){$(this).trigger('mouseenter');}}).blur(function(){if($(this).data('focused')!='n'){$(this).trigger('mouseleave');}});
Now, in IE if you open the select box and move your mouse over one of the options the box closes. What's causing it is the IE apparently doesn't count the dropdown box of options as part of the select element so it triggers the mouseleave event.
Does anyone know a fix around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
特别是 IE 有一个非常奇怪的
不幸的是,
元素上的事件或涉及
元素的事件充其量是不可靠的(就像您所看到的)......并且在 IE 中不能被信任。您可以在 IE 中禁用该行为,但这大约是唯一的“修复”。
全面的替代方案是完全替换
IE in particular has a very bizarre implementation of
<select>
, since IE6 (possibly earlier) it was pulled in from winforms...which is also the reason it sits on top of anything but an<iframe>
in older versions.Unfortunately, events on or involving
<option>
elements are unreliable at best (like you're seeing)...and can't be trusted in IE. You could disable the behavior in IE, but that's about the only "fix" there is.The all-out alternative is to replace the
<select>
completely, there are a few jQuery plugins out there that do this, check out this question for options around that.