2013-07-12 204 views
1

我知道這聽起來很愚蠢,但我想要做的是觸發點擊與一些HTML元素懸停在另一個元素。點擊其他元素上的元素

比方說,我們得到.cursor是懸停錨文本。在這種情況下點擊.cursor應該打開一個谷歌頁面。

<div class="cursor"></div> 
<div class="htmlPage"> 
    <a href="www.google.com">Google</a> 
    <a href="www.facebook.com">Faccebook</a> 
    <a href="www.stackoverflow.com">Stack</a> 
</div> 

任何想法如何做到這一點?

,這不算

$('.cursor').click(function(){ 
    $('.htmlPage a').click(); 
}) 

光標應該可以移動,且應能點擊其他鏈接。

Cursor on google

光標是藍色的圓圈盤旋谷歌按鈕。 在這裏我有光標在谷歌上,現在點擊這個應鏈接到谷歌,如果我點擊堆棧,然後堆棧應該已打開。

+0

你是什麼意思的'光標應該是可移動的,應該能夠點擊其他鏈接?光標將是可移動的,沒有任何限制 – 2013-07-12 16:53:18

+0

我剛剛添加了一張圖片,可以解釋更多。 – Luka

+0

我明白了,我已經更新了我的答案。 – 2013-07-12 17:04:01

回答

2

如果您不使用IE,您可以在CSS中使用pointer-events:none。那麼你的元素將不響應任何鼠標交互(並且像鬼影前景元素一樣)。

對IE的解決方法是成才這樣的:

var x = event.pageX; 
var y = event.pageY; 
$('.cursor').hide(); 
var here = document.elementFromPoint(x, y); 
$('.cursor').show(); 
// Do what you want with the element here 
// Find the parent a element needed with here.parentNode and here.tagName === "A" 
// And then fire the click function 

我從來沒有使用jQuery,但我認爲它應該工作。

希望它可以幫到

+0

我想你甚至可以將onclick函數直接發送到這裏,因爲它會冒泡到元素 – orion78fr

+0

我正要用這個回答我自己的問題。這裏最好的訣竅是你獲得對象,你可以觸發點擊和所有其他鼠標效果。 – Luka

0

試試這個:

Demo

// Target link in the next div, following div.cursor 
$("div.cursor").click(function() { 
    var link = $(this).next(".htmlPage").children("a").attr("href"); 
    window.location.href = link; 
}); 
+0

這裏最大的問題是你無法訪問所有的鏈接。您不能觸發光標下方(下方)的元素。 – Luka

+0

我看到了...希望這個演示會有所幫助 – 2013-07-12 17:15:10

+0

只能有一個遊標。 – Luka

0
$('.cursor').click(function(){ 
    $('.htmlPage a').click(); 
}) 
0

附加一個事件處理光標類。

$('.cursor').on('click',function() 
{ 
    window.location.href = $(this).siblings('.htmlPage').attr('href'); 
} 

這獲取元素的兄弟姐妹,使等於兄弟

0

是有點更明確,這可能是最好的位置。

$('.cursor').click(function(){ 
    $(this).next().children('a').click(); 
}); 
1

你可以嘗試得到點擊「.cursor」的位置,並比較各「.htmlPage一個」位置,並與重疊

$(".cursor").click(function(){ 
    var cursor=$(this); 
    var cl = cursor.offset().left; 
    var cr = cl+cursor.width(); 
    var ct = cursor.offset().top; 
    var cb = ct+cursor.height(); 
    $(".htmlPage a").each(function(){ 
     var page=$(this); 
     var pl = page.offset().left; 
     var pr = pl+page.width(); 
     var pt = page.offset().top; 
     var pb = pt+page.height(); 
     if(((cl>pl&&cl<pr)||(cr>pl&&cr<pr))&&((ct>pt&&ct<pb)||(cb>pt&&cb<pb))){ 
      window.location.href=page.attr("href"); 
     }   
    }); 
}).draggable();  
元素的一個改變window.location.href

http://jsfiddle.net/EUmeB/