2013-03-20 68 views
0

工作,我有以下代碼懸停不使用jQuery 1.9

HTML: 

<div id="test"> 
    <p id="hello">HELLO</p> 
    <p id="no-hello">NO HELLO</p> 
</div> 

的jQuery:

$('#test').click(function() { 
     alert($('#hello').is(':hover')); 
     if ($('#hello').is(':hover')) { 
      alert('hello'); 
     } 
    }); 

我已經使用jQuery 1.6已經和它工作得很好。但現在由於某種原因,1.9被用來代替,並且這已停止工作。 我試圖在網上找到解決方案,但沒有得到任何有關這方面的提示。

Thanx All。

+0

請看看現在 – 2013-03-20 14:37:18

+0

「的jQuery只支持實際選擇的DOM元素選擇 - 一切被忽略。」 [從這些舊文檔](http://docs.jquery.com/API/1.1.2/DOM/Traversing/Selectors)。在較新的文檔中,甚至沒有提及。 – couzzi 2013-03-20 14:37:58

+1

查看您的控制檯 - 它會爲您節省大量時間: '未捕獲的錯誤:語法錯誤,無法識別的表達式:不支持的pseudo:hover' – couzzi 2013-03-20 14:41:33

回答

0

如果您需要在此背景下點擊區域的ID,你可以嘗試:

$('#test').click(function(event) { 
    alert(event.target.id); 
    if (event.target.id == "hello") { 
     alert('hello'); 
    } 
}); 

Sample

但是你提供的方法是不支持的jQuery 1.9+

見一個替代您的方法:

$('#test').click(function() { 
    alert($('#hello:hover').length ==1); 
    if ($('#hello:hover').length) { 
     alert('hello'); 
    } 
}); 

Sample 2

0

As A.V.指出,你的方法在jQuery 1.9+中不起作用。

但是你爲什麼要這樣做呢?

爲什麼不簡單地將兩個不同的點擊事件分配到hellono-hello

function doOnBoth() { ... }; 

$("#hello").click(function() { 
    doOnBoth(); 
    alert('hello'); 
}); 

$("#no-hello").click(function() { 
    doOnBoth(); 
    alert("not hello"); 
}); 

或者你可以指定類兩種元素,然後單擊事件到類,並分別點擊事件的ID。

問題是,有可能觸發點擊而不觸發首先懸停

除非我不明白你想達到什麼......