我有以下文件将通过AJAX加载:
<div id="container">
<div>I need this in AJAX and the following scripts too</div>
<script type="text/javascript">
alert('I am executed');
</script>
<script type="text/javascript" src="some_included_script.js"></script>
</div>
<div>I do not need this in AJAX</div>
如果按以下方式加载:
$.ajax({
url: url,
type: "GET",
cache: false,
dataType: "html",
success: function(data) {
$('#my_big_container').html(data);
}
});
然后执行所有脚本,同时从服务器请求并执行一些包含的脚本.js。没问题。
但如果我只需要“container”中的部分并尝试以下代码:
$.ajax({
url: url,
type: "GET",
cache: false,
dataType: "html",
success: function(data) {
$('#my_big_container').html(
$(data).filter('#container').html()
);
}
});
然后没有脚本被执行,也没有对某些包含的脚本js的请求(尽管“container”的纯HTML内容被正确地插入到“my_big_container”中)。
为什么当我使用整个“数据”而不是过滤其中的一部分时,脚本会被执行?
如何加载接收到的部分数据并自动执行内部脚本?