jQuery:保持& lt; a& gt;可点击div中的可点击链接 [英] jquery: keep <a> link clickable in clickable 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:保持& lt; a& gt;可点击div中的可点击链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!