简体   繁体   English

当href处于另一个单击时会触发事件的类中时,如何使href发挥作用

[英]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.

相关问题 单击按钮时要触发的事件也会触发另一个事件 - Event to be triggered when a button is clicked also triggers another event 单击子项时会触发Jquery Parent Class事件 - Jquery Parent Class event triggers when child items clicked 点击后如何自行更新链接? - How do I make a link update itself when its clicked? 单击div时如何隐藏div并显示另一个? - How do I hide a div when its clicked and show another? 如何使Yii框架的CGridView的linkHtmlOptions显示为图像图标,并在单击时触发javascript / jquery? - how to make the linkHtmlOptions of Yii framework's CGridView appear as an image icon and when clicked, triggers a javascript/jquery? 单击文本时如何在文本上添加类,单击另一图像时如何将其删除,并与其他图像文本相同 - How to add a class on text when clicked to its respective image and remove it when click at another image and do the same with other image text 当子元素触发事件时,如何访问事件的实际目标? - How do I access an event's actual target when a child element triggers the event? 分配其他功能的结果时,如何在Coffeescript中使此更改事件代码起作用? - How do I make this change event code work in Coffeescript when assigning result of another function? 如何使元素在整个 href 卡中不能用作 href - how do I make an element not work as href in an entire href card 将单个单击事件绑定到多个按钮时,如何使被单击的按钮做一件事,而其他所有按钮做另一件事? - When binding a single click event to multiple buttons, how can I make the clicked button do one thing and all the other buttons do another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM