jQuery Div OnClick触发兄弟链接

jQuery Div OnClick trigger sibling link

本文关键字:兄弟 链接 Div OnClick jQuery      更新时间:2023-09-26

我如何使<div class="hover"></div>点击触发<a href="http://example.com" target="_blank"> ?我试过使用兄弟姐妹选择器,我似乎不能得到它的窍门。

当有人点击div.hover时,我需要使用兄弟锚的href打开一个新窗口。

<ul id="photoUL">
<li>
<div class="hover"></div>
<a href="http://example.com" target="_blank">
<img width="150" height="150" style="border: 1px solid #000000;" title="2013 Dirty Duathlon" src="http://example.com/example.jpg">
</a>
<a href="http://example.com" target="_blank">EXAMPLE</a>
</li>
$('div.hover').on('click',function(){
    window.location.href = $(this).next('a').attr('href');
});

Demo ---> http://jsfiddle.net/NGwL9/

Demo: http://jsfiddle.net/kuJPC/

这将打开http://example.com在一个新的选项卡当.hover被点击,将有相同的行为作为<a href="http://example.com" target="_blank">

$('.hover').click(function() {
  window.open('http://example.com','_newtab');
});

如果你不想使用"window. "打开时,您可以在悬停点上放置一个点击处理程序,并使用next函数来模拟下一个锚标记的点击。

$('.hover').click(function(){
    $(this).next('a')[0].click();
});

这是一个工作的jsfield:

http://jsfiddle.net/4FNJM/1/