[英]Clickable DIV with children links
I have created a clickable div
element that has a few links inside it. 我创建了一个clickable
div
元素,其中包含一些链接。 When I click anywhere on the div
the page will go to the mail link but I want to be able to go to all the other links inside that div
. 当我单击
div
上的任意位置时,页面将转到邮件链接,但我希望能够转到该div
内的所有其他链接。 I have managed to do this by calling the e.stopPropagation();
我设法通过调用
e.stopPropagation();
来做到这一点e.stopPropagation();
method. 方法。 This works very good.
效果很好。 You can see it in action here: http://jsfiddle.net/nfZ3y/1/
您可以在此处查看其运行情况: http : //jsfiddle.net/nfZ3y/1/
The problem is, when hold the ctrl
key and click on the link (to open it on a new tab), the link will not work and the page will go to the default link (instead of the one that I just clicked on). 问题是,按住
ctrl
键并单击链接(以在新选项卡上将其打开)时,该链接将不起作用,并且页面将转到默认链接(而不是我刚刚单击的链接)。 How can I achive all of the functionalities of the child links and add a default link for my div
? 如何实现子链接的所有功能并为
div
添加默认链接?
As people pointed out, it seems stopPropagation
works differently in Firefox from the other browsers. 正如人们指出的那样,
stopPropagation
在Firefox中的工作方式似乎与其他浏览器不同。 My only suggestion is handling the click yourself: 我唯一的建议是自己处理点击:
$('.first').click(function (e) {
var title = $(this).children('.main-link');
var href = title.attr('href');
if ( e.ctrlKey )
window.open(href,"_blank");
else
window.location = href;
return false;
});
$('.first a').click(function (e) {
var title = $(this);
var href = title.attr('href');
if ( e.ctrlKey )
window.open(href,"_blank");
else
window.location = href;
return false;
});
Working example on jsFiddle. jsFiddle上的工作示例 。
Update: for less redundancy, substitute the first handler for this: 更新:为了减少冗余,请用第一个处理程序代替:
$('.first').click(function (e) {
$(this).children('.main-link').click();
return false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.