您好我一直在試圖讓這個腳本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