2013-05-11 52 views
8

在做$(...your selector here...).is(":hover"),jQuery的之前1.9.1給出正確的答案,而jQuery的1.9.1告訴你這一點:。是( 「:懸停」)被打破在jQuery 1.9如何解決

Error: Syntax error, unrecognized expression: unsupported pseudo: hover

這裏是(回答沒有必要的。)解決方法

http://jsfiddle.net/mathheadinclouds/V342R/

簡短的回答,檢查是否

.parent().find(":hover") 

具有長度1,並且包含所討論的元素。

這不是關於執行操作懸停 - 對於這一點,只需使用.hover() 這是關於,在任意時間點,找出了是否元件正處於徘徊

+2

你試圖回答自己的問題?如果是這樣做的話:寫一個答案。 – 2013-05-11 13:14:55

+1

你讓事情變得複雜。只需['。.hover()'](http://api.jquery.com/hover/)會:http://jsfiddle.net/BxL4w/5/ – Antony 2013-05-11 13:21:46

+1

@Anthony:錯。我想要一個布爾值,它是否被徘徊。我不想執行懸停操作。 – mathheadinclouds 2013-05-11 13:54:33

回答

12

假設您的選擇器是#myid,請使用$('#myid:hover')而不是使用.is()

如果您使用$(this)或變量,請使用myVar.filter(':hover')

+0

你是對的,那很簡單。我也把它放到小提琴 – mathheadinclouds 2013-05-11 13:53:42

+5

當使用'$(this)'或一個變量對象時怎麼樣? – 2014-02-01 01:36:47

+0

然後,使用 var temp = $(yourElement).parent()。find(「:hover」); return temp.length == 1 && temp [0] == yourElement; – mathheadinclouds 2015-03-14 03:58:18

0

但是,當您使用jQuery.fn.is()與預先未知的組合選擇器比較元素並且可以匹配父容器中的多個元素時,上述解決方法不適用。

例如,當在selectorText style_get(下面)等於同一拋出異常: 「.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懸停不Sizzle.selectors.pseudos或Sizzle.selectors.setFilters定義(jquery的-2.1.4,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; 

} 
+0

你提到的那個「解決方法」是「不合適的」?你在評論另一篇文章嗎?如果是這樣,您應該對該帖子發表評論。你的答案似乎是不完整的。 – Kmeixner 2015-06-01 15:04:33

+0

@Kmeixner,OP寫道:「這是一種解決方法......」它可能是你要求的答案。我用ctrl + f輕鬆找到它並輸入「解決方法」 – Zectbumo 2017-11-30 01:36:42

0

嘗試給匿名功能分爲.filter()

我想,我的解決方案將幫助您:

// 
// 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)); 

也,你可以看到我的GitHub要點:https://gist.github.com/antydemant/74b00e27790e65f4555a29b73eea7bb2