.load()jquery文档

时间:2013-06-17 17:42:52

标签: jquery

我想在用户提交一些内容时将html页面加载到我的容器中。

我用过

$("container").load("page_broken.html",function(responseTxt,statusTxt,xhr){
            if(statusTxt=="success") alert((responseTxt));
        });`

但是page_broken.html<!doctype> <html> <head>(linking to css files and jquery) <body>并注意到我原来的html文件,已经有了这个标签,现在当我用这个html文件加载我的容器时

我担心这些标签被包裹在我的容器内。

http://api.jquery.com/load/这是一个链接,上面写着

jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document. During this process, browsers often filter elements from the document such as <html>, <title>, or <head> elements. As a result, the elements retrieved by .load() may not be exactly the same as if the document were retrieved directly by the browser.

但是当我使用提醒时,要查看回复,它会有<!doctype> <body> and <html>个标记。

那么解决方案是什么,文档是正确的吗?它可能会返回标签,但可能无法加载?任何人都可以清楚地解释我。谢谢

1 个答案:

答案 0 :(得分:1)

.load成功回调的第一个参数是从服务器返回的原始html。如果您的服务器返回doctype head title等,那么原始html也将包含它。 jQuery解析这个html并使用innerHTML将其插入到目标文档中。 innerHTML将从内容中剥离这些标记,因为它们在此时无效。