2010-01-18 82 views
0

我一直在寻找解决问题的日子,但我找不到诀窍...... 当我加载一个外部的PHP页面到div并点击后面的链接来与外部的php代码,这个链接什么都不做。请看看这段代码。jquery没有响应外部加载的页面

的index.php 测试

<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> 

任何想法?

感谢

回答

4

你想要的是使用jQuery的live()方法。见http://api.jquery.com/live/,这将:

“附加处理的事件当前 选择,现在或将来匹配所有 元素。”

因此改变:

$("#test").click(function() { 
    alert("Hello world!"); 
}); 

要:

$("#test").live('click', function() { 
    alert("Hello world!"); 
}); 
+0

+1清晰,简洁! – 2010-01-18 03:26:43

+0

谢谢!这很好,现在我可以将所有处理程序附加到外部加载的页面上。我知道有一个简单的解决方案。 再次感谢! – 2010-01-18 14:39:17