jquery:keep<;a>;可点击分区中的可点击链接

jquery:keep<;a>;可点击分区中的可点击链接,jquery,events,html,clickable,propagation,Jquery,Events,Html,Clickable,Propagation,我想做的是,如果用户单击该div内的链接,则防止单击该div。 但不使用.click(函数(){});在链接上 代码如下: $('div.feature').click(function() { window.location = $(this).attr('rel');}); 下面是div的一个示例内容: <div id="feature" rel="urlnumberone"> some text <a href="javascript:topicchange(this)

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

代码如下:

$('div.feature').click(function() { window.location = $(this).attr('rel');});
下面是div的一个示例内容:

<div id="feature" rel="urlnumberone">
some text
<a href="javascript:topicchange(this);" class="insideLink">Link</a>
</div>
我必须使用这个“topicchange()”函数,有什么方法可以防止事件传播吗


谢谢

您提供的示例应该有效,除了div的id为'feature',但在js中,您针对的是一个类

允许这样做吗?主题更改函数直接从href调用,但会阻止单击事件传播。或者,但一切都发生在topicchange中?

根据,event.stopPropagation应允许执行目标上的其他事件处理程序。您仍然可以从Href执行,但也可以通过忽略默认的单击冒泡到div来处理它

完整演示,网址:

片段:

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.
    }
});
非常类似于@shanetheat

感谢@shannethat要求澄清。


<div id="feature" onclick="location.href='index.php';">
   <a href="anotherpage.php">link</a>
</div>

这么简单

为什么不能使用
$('.insideLink')。单击(主题更改)
函数主题更改(e){e.stopImmediatePropagation();//您的代码如下}
谢谢。但是我说的是我不能使用(.insideLink)。click函数,我必须使用现有的“topicchange()”函数:)对不起,我误解了这个问题。因此,要求调用此topicchange函数,还是必须从href调用?要求是必须从href调用,再次感谢!)您可以在topicChange()中执行xyz,而不是返回false。
$('div#feature').click(function() { 
    alert('div');
});

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

function topicchange(targ) {
    alert('link');
    return false;
}
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.
    }
});
<div id="feature" onclick="location.href='index.php';">
   <a href="anotherpage.php">link</a>
</div>