ajax加载的内容,脚本没有执行

我使用jquery地址插件来加载页面,但没有哈希(#)。

index.html的:

Link 
$(document).ready(function() { $.address.init(function(event) { $('a').address(); }).change(function(event) { // do something depending on the event.value property, eg console.log(event.value); $.ajax({ type: "POST", url: event.value, success: function(msg){ $('#content').html( $(msg).find("#content").html() ); } }); }); $('a').click(function(e) { $.address.value($(this).attr('href')); }); });

https://stackoverflow.com/questions/12359763/ajax-loaded-content-script-is-not-executing/page.html中:

 

text

$(document).ready(function() { alert('loaded'); });

在#content div中将从https://stackoverflow.com/questions/12359763/ajax-loaded-content-script-is-not-executing/page.html加载#content html(也许我应该使用其他函数,而不是.html(),请更正我),因为div是脚本标记,但是当该页面加载时我不会收到警报从ajax,它可以在没有ajax加载的情况下工作。 有人能帮我吗 ?

编辑:当我试图点击链接与jsfunction时,我收到此错误:

XMLHttpRequest cannot load javascript:;. Cross origin requests are only supported for HTTP.

演示: http : //fbstatusi.com/desavanja/kalendar/mesecna_lista

点击链接Zurka 123

document.ready仅在加载document.ready时发生一次。 AJAX请求不会导致它。

 

text

这应该工作。 还尝试将$(msg).find("#content").html()更改$(msg).find("#content")[0].innerHTML ,因为jquery html会删除标记。

编辑

看看这个post ,对于为什么会发生这种情况有一个很长的讨论。 如果像这样$(msg) jquery将始终删除脚本标记。 但同时$(msg).filter("script")返回脚本,因此您可以先找到这些脚本然后在$('#content').html( $(msg).find("#content").html() );之后插入它们$('#content').html( $(msg).find("#content").html() );

我遇到过ajax调用会返回包含需要执行的脚本的新HTML的情况。 代码看起来像这样:

 $.ajax('/url', { method: 'get', success: function(response) { document.getElementById("my-body").innerHTML = response; } }); 

在上面的代码中, response将包含需要执行的脚本,但它不会执行。 将success回调更改为以下修复它:

 $("#my-body").html(response); 

我不确定为什么会这样(显然它与.html()方法的实现有关)。 也许有人可以用解释来评论