[英]How to make an a href do its work when it's inside another class that triggers an event whenver clicked
The question above might not be clear enough, so I will give an example as well: 上面的问题可能还不够清楚,因此我还将举一个例子:
For example, I have the following html code: 例如,我有以下html代码:
<ul class="do-sth">
<li>Line 1</li>
<li>Some text. <a href="http://www.google.com">Line 2.</a> Other text</li>
</ul>
The do-sth
class triggers some even whenver it got clicked. 该
do-sth
级触发有的甚至whenver它得到了点击。 So when I click Line 2
, it will trigger that do-sth
event and at the same time transferring to the href link. 因此,当我单击
Line 2
行时,它将触发该do-sth
事件,并同时转移到href链接。 This is NOT what I want. 这不是我想要的。 What I would like to have is that whenever I click
Line 2
, the do-sth
is not triggered. 我想拥有的是,每当我单击
Line 2
do-sth
都不会触发。 How to do that? 怎么做?
<ul class="do-sth">
<li>Line 1</li>
<li>Some text. <a href="somewhere">Line2</a>. Other text</li>
</ul>
<script>
// try using event.stopPropagation or event.cancelBubble = true for IE
document.querySelector('a').addEventListener('click', function(e)
{
e.stopPropagation();
// do-sth won't be triggered
}, false);
</script>
If what you're asking is to remove the onclick
event from the anchor tag, the below code will work: 如果您要从锚标记中删除
onclick
事件,则以下代码将起作用:
<ul class="do-sth">
<li>Line 1</li>
<li>Some text. <a href="http://www.google.com" onclick="return false">Line 2.</a> Other text</li>
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.