jQuery:保持& lt; a& gt;可点击div中的可点击链接 [英] jquery: keep <a> link clickable in clickable div

查看:256
本文介绍了jQuery:保持& lt; a& gt;可点击div中的可点击链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是,如果用户单击该div内的链接,则防止在div上单击. 但不使用.click(function(){});在链接上.

What I want to do is prevent the click on the div if a users clicks a link inside that div. but without using the .click(function() {}); on the link.

代码如下:

$('div.feature').click(function() { window.location = $(this).attr('rel');});

这是div的示例内容:

here's an example content of the div:

<div id="feature" rel="urlnumberone">
some text
<a href="javascript:topicchange(this);" class="insideLink">Link</a>
</div>

由于某些特定原因我无法使用这样的内容

And for some specific reason I can't use something like this

$('.insideLink').click(function(event){
    event.stopImmediatePropagation();
});

我必须使用此"topicchange()"函数,有什么方法可以防止事件传播?

I have to use this "topicchange()" function, is there any way to prevent the event propagation?

谢谢!

推荐答案

您提供的示例应该可以使用,除了div的id为"feature",但在js中,您的目标是一个类. jsfiddle.net/SNP3K .

The example that you've provided should work, except that your div has an id of 'feature', but in the js you're targeting a class. jsfiddle.net/SNP3K.

<div id="feature" rel="urlnumberone">
    some text
    <a href="#" class="insideLink">Link</a>
</div>

.

$('div#feature').click(function() { 
    alert('div');
});

$('.insideLink').click(function(event){
    event.stopImmediatePropagation();
    alert('link')
});

这篇关于jQuery:保持&amp; lt; a&amp; gt;可点击div中的可点击链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆