2013-07-12 95 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/