如何实现jquery live()而不是每个()

Joa*_*aso 2 javascript jquery popup tooltip

嗨我一直在努力让这个脚本http://jsbin.com/ipajo5/工作,但使用.live()而不是.each(),因为html表是即时填充的.

$(document).ready(function() {

  $('.bi').each(function () {
    // options
    var distance = 10;
    var time = 250;
    var hideDelay = 500;

    var hideDelayTimer = null;

    var beingShown = false;
    var shown = false;

    var trigger = $('.tt', this);
    var popup = $('.popup', this).css('opacity', 0);

    // set the mouseover and mouseout on both element
    $([trigger.get(0), popup.get(0)]).mouseover(function () {
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;    
        popup.css({
          top: $(this).position().top-150,
          left: $(this).position().left-100,
          display: 'block' 
        })
        .animate({
          top: '-=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
          beingShown = false;
          shown = true;
        });
      }
    }).mouseout(function () {
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        popup.animate({
          top: '-=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
          shown = false;
          popup.css('display', 'none');
        });
      }, hideDelay);
    });
  });

});?
Run Code Online (Sandbox Code Playgroud)

注意.在某些线程中,建议使用delegate()而不是live()来提高性能,但是在很多天之后我只希望这个弹出/工具提示能够工作.

谢谢.

Poi*_*nty 5

用".live()"替换".each()"几乎没有意义.提供".each()"工具来迭代已经由选择器匹配的DOM的部分,或者以功能方式通过jQuery对象的组成部分进行迭代.

所有".live()"都可以帮助处理事件.如果您需要在动态加载页面的某些部分时执行其他操作,则必须将其自身组合在"成功"处理程序中以进行动态更新或其他类似操作.