但仍允许事件冒泡

但仍允许事件冒泡

本文介绍了如何防止默认事件触发,但仍允许事件冒泡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jQuery:使用下面的代码我想防止href url(在这种情况下是一个散列'#')被点击,但仍然允许点击事件继续冒泡链。如何实现?

 < div> 
< a href =#>测试< / a>
< / div>

$('a')。click(function(e){
//停止a.click触发,但允许点击事件继续冒泡?


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

e.preventDefault防止冒泡, e.stopPropagation()返回false b

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?
});
解决方案
$('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.

这篇关于如何防止默认事件触发,但仍允许事件冒泡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 06:12