本文介绍了如何加载外部 html 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对使用 iFrame 动态加载外部 html 文件的现有项目进行更改.但是,html 文件是同一个项目的一部分,而不是外部站点.如果我没记错的话,iFrame 被认为是一种糟糕的 html 内容加载方式,除非它们用于实际显示外部网站.

I need to make changes to an existing project that uses iFrames to dynamically load external html files. However, the html files are part of the same project, not external sites. If I'm not mistaken, iFrames are considered a terrible way of loading html content unless they are used to actually display external sites.

我已经研究过 Web 组件,但显然,浏览器支持仍然参差不齐,不幸的是,我需要支持 IE9.

I have looked into web components but apparently, browser support is still spotty and unfortunately, I need to support IE9.

我知道 JQuery load() 方法可以做到这一点,但在我的在线研究中,这通常不是加载外部 html 的正确方法,也不是尤其是 iFrame.

I know that the JQuery load() method can accomplish this but in my online research, that doesn't often come up as a proper way of loading external html in general and a proper replacement for iFrames in particular.

是否有理由不应该在这里使用 JQuery,是否有更好和既定的方法来做到这一点?例如,我曾经看到一个框架,它使用单独的部分"动态构建界面,但我不记得是哪个框架.

Is there a reason why JQuery shouldn't be used here and are there better and established ways of doing this? For example, I once saw a framework that dynamically built the interface out of separate "partials" but I don't remember which framework that was.

推荐答案

这取决于 HTML -

It depends on the HTML -

如果它像完整页面一样构建 - 那么 iFrames 实际上是一个不错的解决方案 - 此外,具有相同来源的 iframes 让您可以完全控制来自父级的内容,同时仍然保护 CSS 和 JS 变量,这非常方便.

If it's built like a full page - then iFrames are actually a decent solution - Also, iframes with the same origin let you have full control over the content from the parent, while still protecting CSS and JS variables which is pretty convenient.

如果没有 - jQuery.load() 可以解决问题,您也可以手动 ofc,但如果您的项目中已经有 jQuery,请使用它.

If not - jQuery.load() will do the trick, you can also do it manually ofc, but if you already have jQuery in your project, just use it.

这篇关于如何加载外部 html 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:34