3 回答

TA贡献1864条经验 获得超2个赞
当load()与片段选择器一起使用时,脚本元素将在添加片段之前被剥离,因此脚本将不会执行。
使用不带后缀选择器表达式的URL调用.load()时,将在删除脚本之前将内容传递给.html()。这将在脚本块被丢弃之前执行它们。但是,如果使用附加到URL的选择器表达式来调用.load(),则在更新DOM之前会删除脚本,因此不会执行这些脚本。两种情况的示例如下所示:
所以尝试
$.get('partial.html', function(result){
$result = $(result);
$result.find('#content').appendTo('#new_content');
$result.find('script').appendTo('#new_content');
}, 'html');

TA贡献1799条经验 获得超6个赞
如果知道要执行的脚本是什么,则可以在ajax响应之外的其他地方定义脚本,然后从回调中调用它。所以代替:
$('#new_content').load(url+' #content > *',function() {alert('returned');});
您可以使用
$('#new_content').load(url+' #content > *',function() {my_function(my_param);});

TA贡献1725条经验 获得超7个赞
这个代码怎么样
$('#new_content').append($('<script>').html(source));
或者您可以简单地将JavaScript内容移动到单独的文件中,然后使用
$.getScript("url to js file");
或只是使用,
eval(source);
添加回答
举报