且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

脚本通过.load()加载失败

更新时间:2023-12-05 08:25:58

在与加载.load()和使用的URL后,选择喜欢你正在做的,jQuery的根本不运行加载的内容的脚本。它明确地剥离出来,并引发他们离开。

When you load with .load() and use a selector after the URL like you're doing, jQuery simply does not run any scripts in the loaded content. It explicitly strips them out and throws them away.

我觉得它这样做的主要原因是,由于它扔掉这就是被加载的网页的某一部分,它已经不知道该嵌入的脚本是所选择的内容中是否会工作,没有其他的JavaScript的网页的上没有的选择。

I think the main reason it does this is that, since it's throwing away some portion of the page that's been loaded, it has no idea whether the embedded scripts that are inside the selected content will work without other JavaScript on the page that's not selected.

修改的—很遗憾,您的替代code将遭遇同样的问题。问题是, $(数据)包括将返回的HTML页面到文档片段。也就是说,内部jQuery的,会经过一个所谓的干净的功能,它删除了脚本。它确实救他们,但他们的时候扔掉了 $(数据)函数调用返回。

edit — unfortunately your substitute code will suffer from the same problem. The issue is that $(data) involves converting the returned page HTML into a document fragment. That, internal to jQuery, will go through a function called "clean" that strips out the scripts. It does save them, but they're thrown away by the time that the $(data) function call returns.

它可能会是顺畅了很多,如果你能想出办法为你的服务器的做搞清楚,它并没有返回一个完整的网页的工作。这会让事情更快呢;有没有点发货,当你打算只使用其中的一部分一个完整的页面。

It's probably going to be a lot smoother if you can figure out a way for your server to do the work of figuring out that it doesn't have to return a complete page. It'll make things faster anyway; there's no point shipping a complete page when you're going to only use a portion of it.