如何让一个 href 在另一个 class 内工作,当点击时触发事件

How to make an a href do its work when it's inside another class that triggers an event whenver clicked

上面的问题可能不够清楚,我再举个例子:

例如,我有以下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>

do-sth class 即使在点击时也会触发一些。所以当我点击 Line 2 时,它会触发那个 do-sth 事件,同时转移到 href link。这不是我想要的。我想要的是,每当我单击 Line 2 时,都不会触发 do-sth。怎么做?

<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>

如果您的要求是从锚标记中删除 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>