jQuery 从外部 HTML 加载正文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5271316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:52:02  来源:igfitidea点击:

jQuery load body from external HTML

jqueryhtmlload

提问by Diogo Cardoso

I'm using jQuery and trying to load the body from an external HTML file.

我正在使用 jQuery 并尝试从外部 HTML 文件加载正文。

It works if I try to load some div:

如果我尝试加载一些,它会起作用div

$('body').load('example.html #content');

$('body').load('example.html #content');

But I'm unable to do something like this:

但我无法做这样的事情:

$('body').load('example.html body');

$('body').load('example.html body');

回答by Adam Hopkinson

From http://api.jquery.com/load

来自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.

jQuery 使用浏览器的 .innerHTML 属性来解析检索到的文档并将其插入到当前文档中。在此过程中,浏览器通常会过滤文档中的元素,例如 <html>、<title> 或 <head> 元素。因此, .load() 检索到的元素可能与浏览器直接检索文档时不完全相同。

Reading the above, it seems possible that your browser be only returning the body innerHTML. This obviously should be the same as requesting body, but maybe it's causing an error because bodyis not found?

阅读以上内容,您的浏览器似乎可能只返回正文 innerHTML。这显然应该与 requests 相同body,但也许它会导致错误,因为bodyis not found?

I'd suggest trying a different browser.

我建议尝试不同的浏览器。

回答by Réj?me

If you want to load the whole body, $('body').load('example.html');should be enough.

如果要全身装,$('body').load('example.html');应该够了。

If not, could you explain why ?

如果没有,你能解释一下为什么吗?

Anyway, the solution provided by Steve is the right one.

无论如何,史蒂夫提供的解决方案是正确的。

回答by webcoder

I guess what you need is to add an iframe. Pass the URL to its src property. Your whole html will get loaded in this iframe then. A clean and simple solution for your problem. All the best :-)

我想你需要的是添加一个 iframe。将 URL 传递给其 src 属性。然后你的整个 html 将被加载到这个 iframe 中。为您的问题提供干净简单的解决方案。祝一切顺利 :-)

回答by jpaugh

When you use load without a selector, it strips the <html>, <head>, and <body>tags, but leaves all of the children of <head>and <body>intact, including title, script, and style tags. However, when you use a selector, it is applied afterthe body tag is stripped, so it's impossible to match against it.

当您使用负载没有选择,它去除的<html><head>以及<body>标签,但叶子所有的儿童<head><body>完整,包括标题,脚本和风格标签。但是,当您使用选择器时,它是body 标记被剥离应用,因此无法与之匹配。

Instead, wrap all of your body content inside another tag, and select that tag instead:

相反,将所有正文内容包装在另一个标签中,然后选择该标签:

<html><head><title>Hello!</title</head>
<body>
  <div id="jquery-load-point">
    ... <!-- rest of body content -->
  </div>
</body>
</html>

Now, you can load it like this:

现在,您可以像这样加载它:

$("#destination").load("http://my.url/to/page #jquery-load-point");

回答by Hussein

Make sure you are using jQuery 1.5.1 and not 1.5. The load feature in 1.5 had issues. I answered a similar questions at jquery .load() doesn't work

确保您使用的是 jQuery 1.5.1 而不是 1.5。1.5 中的加载功能有问题。我在jquery .load() 上回答了类似的问题不起作用

Another thing is to try it in different browsers to make sure it's not a browser issue. Chrome doesn't allow you to use load when working from localhost without doing some tweaking.

另一件事是在不同的浏览器中尝试它以确保它不是浏览器问题。Chrome 不允许您在不做一些调整的情况下从本地主机工作时使用负载。