我一直在寻找解决问题的日子,但我找不到诀窍...... 当我将一个外部的PHP页面加载到div中,然后在链接上单击外部PHP代码时,此链接不执行任何操作。请看一下这段代码。
的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>
有什么想法吗?
由于
答案 0 :(得分:4)
你想要的是使用jQuery的live()
方法。请参阅http://api.jquery.com/live/,其中:
“为所有人附加事件的处理程序 与当前相匹配的元素 选择器,现在或将来。“
所以改变:
$("#test").click(function() {
alert("Hello world!");
});
要:
$("#test").live('click', function() {
alert("Hello world!");
});