Javascript 如何检测是否在DIV内单击了特定的类链接并覆盖DIV';那我们点击行动吧?

Javascript 如何检测是否在DIV内单击了特定的类链接并覆盖DIV';那我们点击行动吧?,javascript,jquery,html,onclick,Javascript,Jquery,Html,Onclick,我的页面上有以下几种DIV: <div class='entry'> This is a statement <a title="Search @travel" class="app-context-link" href="">@travel</a> </div> 单击class.app上下文链接的链接时,我会触发以下事件: $(".entry").on('click', function(e) { console.log

我的页面上有以下几种DIV:

<div class='entry'>
    This is a statement 
    <a title="Search @travel" class="app-context-link" href="">@travel</a>
</div>
单击class.app上下文链接的链接时,我会触发以下事件:

$(".entry").on('click', function(e) {
   console.log("DIV Clicked");
});
var context_links = document.getElementsByClassName('app-context-link');

for (var k=0;k<context_links.length;++k) {
    context_links[k].addEventListener('click', function(e) {
        console.log("The context link inside DIV is clicked");
    });
}
var context_links=document.getElementsByClassName('app-context-link');

对于(var k=0;k使用停止传播:

context_links[k].addEventListener('click', function(e) {
    e.stopPropagation(); //Here
    console.log("The context link inside DIV is clicked");
});

这将阻止单击事件冒泡,因此当您单击
a
时,不会触发其祖先的单击事件。

使用停止传播:

context_links[k].addEventListener('click', function(e) {
    e.stopPropagation(); //Here
    console.log("The context link inside DIV is clicked");
});
这将阻止单击事件冒泡,因此当您单击
a
时,不会触发其祖先的单击事件。

您可以这样使用

$(".entry").on('click', function(e) {
e.stopPropagation();
alert($(this).prop("tagName"));
});
$(".entry a").on('click', function(e) {
e.preventDefault();
 e.stopPropagation();
alert($(this).prop("tagName"));
});
您可以这样使用

$(".entry").on('click', function(e) {
e.stopPropagation();
alert($(this).prop("tagName"));
});
$(".entry a").on('click', function(e) {
e.preventDefault();
 e.stopPropagation();
alert($(this).prop("tagName"));
});

既然您使用的是jQuery,为什么不将其用于链接事件处理程序?只需使用$('.entry')。在('click','.app context link',函数(e){…})上;这将向您的
.entry
中添加一个委托处理程序,该处理程序将处理div中所有
.app context link
的单击,而不是为每个链接添加一个处理程序。既然您正在使用jQuery,为什么不将其用于链接事件处理程序?只需使用$('.entry')。on('click','.app context link',函数(e){…});这将向您的
.entry
中添加一个代理处理程序,该处理程序将处理div中所有
.app context link
的单击,而不是为每个链接添加一个处理程序。