.is(":hover") 自 jQuery 1.9 起已损坏 如何修复
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16497457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.is(":hover") is broken as of jQuery 1.9 How to fix
提问by mathheadinclouds
When doing $(...your selector here...).is(":hover")
, jQuery prior to 1.9.1 gave correct answer, while jQuery 1.9.1 tells you this:
这样做时$(...your selector here...).is(":hover")
,1.9.1 之前的 jQuery 给出了正确答案,而 jQuery 1.9.1 会告诉您:
Error: Syntax error, unrecognized expression: unsupported pseudo:
hover
错误:语法错误,无法识别的表达式:不支持的伪:
hover
This is not about performing an action on hover - for that, just use .hover() This is about, at an arbitrary point in time, finding out whether or not some element is being hovered
这不是关于在悬停时执行操作 - 为此,只需使用 .hover() 这是关于在任意时间点找出某个元素是否被悬停
Thank you Mooseman for the answer, which I shall demonstrate with a fiddle
感谢 Mooseman 的回答,我将用小提琴演示
回答by Mooseman
Assuming your selector is #myid
, use $('#myid:hover')
instead of using .is()
.
假设您的选择器是#myid
,请使用$('#myid:hover')
而不是使用.is()
。
If you are using $(this)
or a variable, use myVar.filter(':hover')
.
如果您使用$(this)
或 变量,请使用myVar.filter(':hover')
.
回答by luxigoo
However the workaround above is unsuitable when you are comparing elements using jQuery.fn.is() with a composed selector which is not known beforehand, and that could match several elements in the parent container.
但是,当您将使用 jQuery.fn.is() 的元素与事先未知的组合选择器进行比较时,上述解决方法是不合适的,并且可以匹配父容器中的多个元素。
eg, the same exception is thrown when selectorText in style_get() below is equal to: ".mCSB_scrollTools_vertical.mCSB_scrollTools_onDrag_expand .mCSB_dragger.mCSB_dragger_onDrag_expanded .mCSB_dragger_bar, .mCSB_scrollTools_vertical.mCSB_scrollTools_onDrag_expand .mCSB_draggerContainer:hover .mCSB_dragger .mCSB_dragger_bar"
例如,当在style_get selectorText(下面)等于同一抛出异常: “.mCSB_scrollTools_vertical.mCSB_scrollTools_onDrag_expand .mCSB_dragger.mCSB_dragger_onDrag_expanded .mCSB_dragger_bar,.mCSB_scrollTools_vertical.mCSB_scrollTools_onDrag_expand .mCSB_draggerContainer:悬停.mCSB_dragger .mCSB_dragger_bar”
..because hover is not defined in Sizzle.selectors.pseudos or Sizzle.selectors.setFilters (jquery-2.1.4, line 1764)..
..因为悬停在 Sizzle.selectors.pseudos 或 Sizzle.selectors.setFilters (jquery-2.1.4, line 1764) 中没有定义..
function style_get(elem) {
var sheets=document.styleSheets;
var css_properties={};
elem=$(elem);
for (var s in sheets) {
var rules=sheets[s].rules || sheets[s].cssRules;
for (var r in rules) {
if (elem.is(rules[r].selectorText)) {
css_properties=$.extend(
css_properties,
css.parse(rules[r].style),
css.parse(elem.attr('style'))
);
}
}
}
return css_properties;
}
回答by Igor Ostapchuk
You could try to use .filter()
你可以尝试使用 .filter()
I think, my solution will help you:
我认为,我的解决方案将帮助您:
//
// jQuery 3 pseudo ':hover' doesn't support
//
// Working on jQuery 1.7.1
$("*").on('click', $.proxy(function() {
if ( this.$('.tooltip:hover').length > 0 ) { // jQuery 3.2.1 -> Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: hover
console.log('Hovered!');
} else {
console.log('Where is my element!?');
}
}, this));
// Updated solution for jQuery 1.7.1 - 3.2.1
$("*").on('click', $.proxy(function() {
var isHovered = $('.tooltip').filter(function() {
return $(this).is(":hover");
});
if ( isHovered.length > 0 ) {
console.log('Hovered!');
} else {
console.log('Where is my element!?');
}
}, this));
also, you can see my GitHub gist:https://gist.github.com/antydemant/74b00e27790e65f4555a29b73eea7bb2
另外,您可以查看我的 GitHub 要点:https: //gist.github.com/antydemant/74b00e27790e65f4555a29b73eea7bb2