Jquery not responding for external loaded page
I've been lookings days for solving a problem, but i can not find the trick.. When i load an external php page into a div and click afterward on a link come with the external php code, this link does nothing. Please take a look at this code.
index.php 开发者_如何学编程 Test
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
$("#loadexternfile").bind("click", function(){
// some more events
loadContent();
});
$("#test").click(function() {
alert("Hello world!");
});
function loadContent() {
$.ajax({
type: "GET",
url: "external.php",
cache: false,
dataType: 'html',
success: function(html){
$(".loaddiv").html(html);
},
error: function(){
},
complete: function(){
}
});
}
});
</script>
</head>
<body>
<a href="#" id="loadexternfile">loadexternfile</a>
<div class="loaddiv"></div>
</body>
</html>
external.php
<a href="#" id="test">test</a>
Any ideas?
Thanks
What you want is to use the live()
method of jQuery. See http://api.jquery.com/live/, which will:
"Attach a handler to the event for all elements which match the current selector, now or in the future."
So change:
$("#test").click(function() {
alert("Hello world!");
});
To:
$("#test").live('click', function() {
alert("Hello world!");
});
精彩评论