V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
blacksu
V2EX  ›  JavaScript

在读<Async Javascript>(《JavaScript 异步编程》),请教一个小问题

  •  
  •   blacksu · 2015-06-04 10:00:40 +08:00 · 2526 次点击
    这是一个创建于 3250 天前的主题,其中的信息可能已经有所发展或是发生改变。
    书中讲到jQuery自定义事件时举了这样一个例子,使用自定义事件来移除container中的tooltip,而别的container中的tooltip不受影响:

    请问代码中的$tooltip是什么?为什么这样做就会 each tooltip will listen to its container?跟事件冒泡有关吗?

    中文版在此: http://www.itxueyuan.org/view/6933.html

    谢谢。

    But implementing this behavior with event logic rather than selector logic is easy.

    // $container could be $('#sidebar') or $(document)

    $container.triggerHandler('newTooltip');

    $container.one('newTooltip', function() {

    $tooltip.remove();

    });
    (Notice the use of jQuery’s one instead of on. The difference is that one automatically removes the handler after it fires.)

    With these two lines of code, each tooltip will listen to its container and remove itself when the container gets a new tooltip.
    第 1 条附言  ·  2015-06-04 12:54:06 +08:00
    如果例子中的代码是错的,要用自定义事件实现这样的功能应该怎么做呢?
    6 条回复    2015-06-04 14:40:10 +08:00
    icellent
        1
    icellent  
       2015-06-04 10:07:06 +08:00
    jQ支持使用自定义事件,对tooltip做自定义事件,以后要移除制定的时间,只需做个trigger的动作,而不会影响到因使用$('.tooltip').remove() 这个方法导致移除所有的tooltip。。
    不知道这样说正不正确,有点头晕,哈哈
    czheo
        2
    czheo  
       2015-06-04 10:16:31 +08:00
    这例子写的太不清楚,建议跳过。
    emric
        3
    emric  
       2015-06-04 10:23:33 +08:00
    例子想说明的是自定义事件的用法, 新建一个事件, 然后触发.
    $tooltip 是示例代码, 和事件没有关系.
    Niphor
        4
    Niphor  
       2015-06-04 14:33:44 +08:00
    $tooltip 应该是指任意一个tooltip的实例,这边应该是想表达:
    注册个自定义事件`newTooltip`,任何一个tooltip在实例化时,都需要先触发一下自定义事件,然后用one方法在$container上注册下handler。以便于在规定的$container内同一时间只有1个tooltip显示.
    Niphor
        5
    Niphor  
       2015-06-04 14:37:06 +08:00
    我感觉这边更主要的介绍triggerHandler 和 trigger 的不同,triggerHandler 不能冒泡,所以能实现container的事件隔离...
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   871 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:49 · PVG 04:49 · LAX 13:49 · JFK 16:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.