Javascript 导入 html 有可能吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4818854/
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-23 14:15:52  来源:igfitidea点击:

Javascript import html is it possible?

javascripthtml

提问by ulima69

I have some html pages with the same footer. With javascript and only javascript could i import another html page inside it?

我有一些具有相同页脚的 html 页面。使用 javascript 并且只有 javascript 我可以在其中导入另一个 html 页面吗?

回答by Ricky

As above, one method is to use jQuery load. I happened to be doing the exact same thing now, so will post a quick example.

如上所述,一种方法是使用 jQuery 加载。我碰巧现在正在做完全相同的事情,因此将发布一个快速示例。

Using jQuery:

使用jQuery:

$("#yourDiv").load('readHtmlFromHere.html #readMe');

And your readHtmlFromHere.html page would consist of:

您的 readHtmlFromHere.html 页面将包括:

<div><div id="readMe"><p>I'm some text</p></div></div>

回答by gilly3

Here's how you could use just javascript to add a footer to your page.

以下是如何仅使用 javascript 向页面添加页脚的方法。

var ajax = new XMLHttpRequest();
ajax.open("GET", "footer.htm", false);
ajax.send();
document.body.innerHTML += ajax.responseText;

回答by Alex

You can use ajax to return a whole HTML page. If you wanted to replace the whole page you could replace the body tag and all it's children currently on the page with the body tag returned from the ajax call.

您可以使用 ajax 返回整个 HTML 页面。如果您想替换整个页面,您可以使用从 ajax 调用返回的 body 标记替换当前页面上的 body 标记及其所有子项。

If you wanted to just replace a section you'd have to write a server-side script to create that section, then use ajax as above but just replace an element rather than the whole page.

如果您只想替换一个部分,您必须编写一个服务器端脚本来创建该部分,然后使用上面的 ajax,但只替换一个元素而不是整个页面。

回答by Brad Christie

Along with what @Alex mentioned, jQueryhas a method .load()that you can use to fetch a specific portion of a page (See Loading Page Fragments heading on that page). You specify the URL you want to retrieve along with a selector (so if you wanted only a specific <DIV>'s contents for instance).

除了@Alex 提到的内容之外,jQuery还提供了一种方法.load(),您可以使用它来获取页面的特定部分(请参阅加载该页面上的页面片段标题)。您指定要与选择器一起检索的 URL(例如,如果您只想要特定<DIV>的内容)。

回答by Stephan

You could do a server side include, depending on your webserver. but the quickest way would probably be to create a JavaScript file that uses document.write or similar to output the html syntax. and then just include the created JavaScipt file the normal way. more info at: http://webdesign.about.com/od/ssi/a/aa052002a.htm

您可以执行服务器端包含,具体取决于您的网络服务器。但最快的方法可能是创建一个使用 document.write 或类似的 JavaScript 文件来输出 html 语法。然后以正常方式包含创建的 JavaScipt 文件。更多信息请访问:http: //webdesign.about.com/od/ssi/a/aa052002a.htm

回答by Andrew Noyes

You definitely could, but if all you're doing is templating I recommend you do this on the server.

您绝对可以,但如果您所做的只是模板化,我建议您在服务器上执行此操作。