防止“冒泡"? [英] Prevent "bubbling"?

查看:84
本文介绍了防止“冒泡"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这真的在冒泡,我会解释.

I am not sure this is really bubbling, I will explain.

我有这个:

<div>
  <div>
    text here
  </div>
</div>

如何绑定一个单击事件,使其仅影响所附的div?如果我这样设置:

How to bind an on click event so that it will affect only the enclosed div? If I set it like this:

jQuery('div').bind('click', function() {
  jQuery(this).css('background','blue');
});

使所有div变为蓝色.如果我在绑定函数中添加false作为第三个参数(防止冒泡),则不会执行任何操作.

it makes blue all the divs. If I add false as the third argument(prevent bubbling) to the bind function it does nothing.

我该如何解决?

推荐答案

http://api.jquery .com/event.stopPropagation/

在处理程序内部添加event.stopPropagation();.

(不过,将ID或类分配给嵌套的DIV可能会更好,因此可以确保它是唯一受影响的一个.)

(It might be better, though, to assign an ID or class to the nested DIV so you can be sure it's the only one affected.)

这篇关于防止“冒泡"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆