2010-05-18 59 views
1

我使用jQuery 1.3.2和Live Query插件。腳本需要在FF和IE6中工作。升級jQuery並使用live不是一種可能性。使用livequery:無法找到通過ajax創建的傳播元素

不知何故,這個腳本不會被動態創建的元素調用。

$('select').livequery('change',function(){ 
     var select_id = $(this).attr("id");                   ... 
... 
... 

}); 

$('select').livequery('mouseover',hideExtensions()); 

function hideExtensions(){ 
... 
... 
} 

在IE6和FF中,靜態(已存在的)元素正確調用該函數。但是,它不會被動態創建的元素調用。

可能是什麼原因?

更新 我測試了與「活」相同的功能。它在FF中工作,但不是在IE6中,當然不是......這就是爲什麼我要用livequery尋找解決方法。

回答

0

有沒有理由不能升級到jQuery 1.4.2並在那裏使用內置的live回調?

如果livequery無法正常工作,您可以嘗試的另一種方法是使用一些核心JavaScript進行一些「手動」事件委託,並依靠事件冒泡到直接父級。比方說,你有一個DOM結構是這樣的:

<div id='something'> 
    <a href='#' class='some-tag'></a> 
    <a href='#' class='some-tag'></a> 
    <a href='#' class='some-tag'></a> 
    <!-- more dynamic elements 'a' tags identical to those above added here --> 
</div> 

而且在你的JavaScript:

var something = document.getElementById('something'); 

something.addEventListener('click', function(e) { 
    e = window.event || e; 

    // If the target of the event is the added anchor we're looking for 
    if (e['srcElement' in e ? 'srcElement' : 'target'].getAttribute('class') === 'some-tag') 
     // code block goes here 

    return false; 

}, false); 
+0

感謝您的評論。我不想更新到1.4.2的原因是我們的程序可能會中斷。 – Kel 2010-05-18 17:34:09