How to prevent default event firing but still allo

2019-06-16 17:13发布

Using jQuery: with the following code I'd like to prevent the href url (in this case a hash '#') being fired on click, but still allow the click event to continue bubbling up the chain. How can this be achieved please?

<div>
    <a href="#">Test</a>
</div>

$('a').click(function(e){
    // stop a.click firing but allow click event to continue bubbling?
});

3条回答
女痞
2楼-- · 2019-06-16 17:54

try this..

$('a').click(function(e){
     e.preventDefault();
    // stop a.click firing but allow click event to continue bubbling?

});

here you can find difference between return false and e.preventDefault();

查看更多
Luminary・发光体
3楼-- · 2019-06-16 18:06
$('a').click(function(e){
    e.preventDefault();
    // stop a.click firing but allow click event to continue bubbling?
});

e.preventDefault() won't prevent bubbling, e.stopPropagation() or return false (stop both) will.

查看更多
Bombasti
4楼-- · 2019-06-16 18:07

You could do:

$('a').click(function(e){
     e.preventDefault();
    // stop a.click firing but allow click event to continue bubbling?

});

fiddle here http://jsfiddle.net/tU53v/

查看更多
登录 后发表回答