简体   繁体   English

jquery:在可点击的div中保持<a>链接可点击

[英]jquery: keep <a> link clickable in clickable div

What I want to do is prevent the click on the div if a users clicks a link inside that div. 我想要做的是阻止点击div,如果用户点击该div内的链接。 but without using the .click(function() {}); 但是没有使用.click(function(){}); on the link. 在链接上。

Here's the code: 这是代码:

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

here's an example content of the div: 这是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();
});

I have to use this "topicchange()" function, is there any way to prevent the event propagation? 我必须使用这个“topicchange()”函数,有什么方法可以阻止事件传播?

Thanks! 谢谢!

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. 您提供的示例应该有效,但您的div的ID为“feature”,但在js中您定位的是一个类。 jsfiddle.net/SNP3K . 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')
});
<div id="feature" onclick="location.href='index.php';">
   <a href="anotherpage.php">link</a>
</div>

Easy as that! 很容易!

According to http://api.jquery.com/bind/ , event.stopPropagation should allow other event handlers on the target to be executed. 根据http://api.jquery.com/bind/,event.stopPropagation应该允许执行目标上的其他事件处理程序。 You can still execute from the Href but you can also handle the default click bubbling to the div by ignoring it. 您仍然可以从Href执行,但您也可以通过忽略它来处理默认的点击冒泡到div。

Full demo at: http://jsfiddle.net/leomwa/qrFQM/ 完整演示: http//jsfiddle.net/leomwa/qrFQM/

Snippet: 片段:

function topicChange()
{
    alert('Message from Link: After the div mouse event, I can execute');
    return false;
}

$('div#feature').click(function(evnt)
{
    var $originatingTarget = $(evnt.target);
    if ($originatingTarget && $originatingTarget.is('.insideLink'))
    {
        alert('Message from Div: Ahh! You clicked on Link...');
        evnt.stopPropagation(); // Allows other handlers on the event.target to be executed.
    }
});

Very similar to @shanethehat. 和@shanethehat非常相似。

Kudos to @shanethehat for asking for a clarification. 感谢@shanethehat要求澄清。

$(document).ready(function() {
    $('div#feature').click(function() {
        alert('div');
    });
    $('div#feature a').click(function(evt) {
        evt.stopPropagation();
    });
});

function topicchange(targ) {
    alert('link');
    return false;
}

Is this allowed? 这是允许的吗? The topic change function is called directly from the href, but the click event is prevented from propagating. 主题更改函数直接从href调用,但阻止click事件传播。 Or but everything take place in topicchange? 或者一切都发生在topicchange中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM