2010-11-01 53 views
0

我有以下腳本;問題是,我可以使用加載的文件加載jquery裏面加載文件的問題

的index.html內的jQuery腳本

<html> 
<head> 
<title>Ajax with jQuery Example</title> 
<script type="text/JavaScript" src="jquery.js"></script> 
<script type="text/JavaScript"> 
$(document).ready(function(){ 
$.ajax({ 
    url: 'ajax/test.html', 
    success: function(data) { 
    $('#wrapper').html(data); 
    } 
}); 

    $("A").click(function(){ 
    alert("test inside loaded file"); 
    }); 

}); 
</script> 
</head> 
<body> 
<div id="wrapper"> 
</div> 
</body> 
</html> 

AJAX/test.html的

<A HREF="document2.html" TABINDEX="4">test alert</A> 

他會給毫無戒備當我點擊的問題在加載的文件上

回答

1

使用

$("A").live("click", function(){ 
    alert("test inside loaded file"); 
}); 
+0

CD擊敗了我:) – baruch 2010-11-01 11:49:58

2

嘗試改變下面的行:

$("A").click(function(){ 
    alert("test inside loaded file"); 
    }); 

$("A").live('click',function(){ 
    alert("test inside loaded file"); 
    }); 

通過使用Live()方法喲您現在和將來都會綁定到所有<a>標籤,使用.click()僅綁定到當前存在的匹配元素。

+0

這個作品我有一個類似的問題。 - 謝謝! – cocacola09 2010-12-20 23:44:24