2011-03-09 20 views
1

我正在通過ajax加載一些內容,並且需要使用live()函數。既然下面的插件有屬性來設置你如何設置它以使用live()如何在此插件上使用jQuery .live

$(function() { 
     $("#book-wrapper .books").hoverIntent({ 
      over: enter, 
      out: leave, 
      interval: 200 
     }); 
    }); 
    function enter() { $(this).animate({ width: '+=115px' }, 350, 'swing'); $(".slide-out", this).animate({ opacity: 1 }, 450, 'swing'); } 
    function leave() { $(this).animate({ width: '-=115px' }, 350, 'swing'); $(".slide-out", this).animate({ opacity: 0.0 }, 450, 'swing'); } 
+0

你是不是想給'hoverIntent'方法添加到所有新的'.books'? – philwinkle 2011-03-09 05:54:58

+0

@philwinkle,是的,在頁面加載時,不會有類型爲「.books」的元素。他們將通過ajax加載,然後我需要他們對hoverIntent做出反應。 – 2011-03-09 05:56:02

回答

2

鑑於你上面的澄清 - 你實際上可能不需要live ......爲bind可以實現這個要求。其他一些插件存在這這樣做,以及包括livequery

$(function(){ 
    $('html').bind('myhoverintent',function(){ 
     $('#book-wrapper .books').hoverIntent({ 
      over: enter, 
      out: leave, 
      interval: 200 
     }); //fixed syntax error 
}); 

//then, later in your method that adds .books: 

$('html').append('<div class="books">test</div').trigger('myhoverintent'); 

即使是簡單的是,如果你的方法來添加圖書是作爲一個jQuery的AJAX方法的結果,你可以只是重新打電話給你的原始選擇的$.ajax

+0

@philwinkle,ready函數中的部分不會導致javascript在應用程序中工作。 – 2011-03-09 06:04:18

+0

我實際上發現了一個語法錯誤並更新了。看到這裏的一些僞代碼示例:http://jsfiddle.net/philwinkle/XQaRN/ – philwinkle 2011-03-09 06:06:44

+0

@philwinkle,我甚至沒有考慮將它放在'$ .ajax'成功函數中,duh:/ – 2011-03-09 06:09:16

-2

success方法試試這個,

$(function() { 
     $("#book-wrapper .books").live("hoverIntent",function(){ 
      over: enter, 
      out: leave, 
      interval: 200 
     }); 
    }); 
    function enter() { $(this).animate({ width: '+=115px' }, 350, 'swing'); $(".slide-out", this).animate({ opacity: 1 }, 450, 'swing'); } 
    function leave() { $(this).animate({ width: '-=115px' }, 350, 'swing'); $(".slide-out", this).animate({ opacity: 0.0 }, 450, 'swing'); } 
+0

函數內部的參數會認爲它們是你的語法的變量,這是行不通的。 – 2011-03-09 06:10:08

+0

@Nick。 。是的,沒有注意到函數內部的參數。對不起:) – kushalbhaktajoshi 2011-03-09 06:12:39

相關問題