推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
blacksu

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

  •  
  •   blacksu · Jun 4, 2015 · 3370 views
    This topic created in 3996 days ago, the information mentioned may be changed or developed.
    书中讲到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.
    Supplement 1  ·  Jun 4, 2015
    如果例子中的代码是错的,要用自定义事件实现这样的功能应该怎么做呢?
    6 replies    2015-06-04 14:40:10 +08:00
    icellent
        1
    icellent  
       Jun 4, 2015
    jQ支持使用自定义事件,对tooltip做自定义事件,以后要移除制定的时间,只需做个trigger的动作,而不会影响到因使用$('.tooltip').remove() 这个方法导致移除所有的tooltip。。
    不知道这样说正不正确,有点头晕,哈哈
    czheo
        2
    czheo  
       Jun 4, 2015
    这例子写的太不清楚,建议跳过。
    emric
        3
    emric  
       Jun 4, 2015
    例子想说明的是自定义事件的用法, 新建一个事件, 然后触发.
    $tooltip 是示例代码, 和事件没有关系.
    Niphor
        4
    Niphor  
       Jun 4, 2015
    $tooltip 应该是指任意一个tooltip的实例,这边应该是想表达:
    注册个自定义事件`newTooltip`,任何一个tooltip在实例化时,都需要先触发一下自定义事件,然后用one方法在$container上注册下handler。以便于在规定的$container内同一时间只有1个tooltip显示.
    Niphor
        5
    Niphor  
       Jun 4, 2015
    我感觉这边更主要的介绍triggerHandler 和 trigger 的不同,triggerHandler 不能冒泡,所以能实现container的事件隔离...
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5652 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 87ms · UTC 07:31 · PVG 15:31 · LAX 00:31 · JFK 03:31
    ♥ Do have faith in what you're doing.