您好我一直在试图让这个脚本http://jsbin.com/ipajo5/工作,但使用.live(),而不是.each(),因为html表中填写动态。如何实现jquery live()而不是每个()
$(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);
});
});
});
注意。在一些线程中,推荐使用delegate()来代替live()来实现性能,但是在很多天之后,我只想让这个popup/tooltip工作。
谢谢。
我知道。每个()是遍历一个jQuery对象,为每个匹配的元素执行一个函数,而live()是为现在和将来匹配当前选择器的所有元素的事件附加一个处理函数。所以也许我的问题不是非常具有描述性,我想让脚本能够正常工作,但是我无法附加脚本的功能,以便在html元素上重新创建,而不是与每个元素匹配,并且可能使用live ) – 2011-01-24 04:46:09
@JoaquínDuaso好的 - 那你想做什么? – Pointy 2011-01-24 04:47:01