HTML 不支持客户端包含其他 HTML,例如使用 C 获取#include
指示。
相反,客户端 HTML 包含的主要工具似乎是iframe
, object
,尤其是 jQuery 的.load
.
See 这个线程 https://stackoverflow.com/questions/8988855/include-another-html-file-in-a-html-file举些例子。
不幸的是,这些方法似乎都不能产生与从 DOM 中获得的完全相同的 DOM。#include
。
尤其,iframe
and object
将包含的内容包装在各自的标签中。
此外,示例线程中提出的 jQuery 解决方案仍然会导致 DOM 带有额外的div
.
例如,考虑线程的 jQuery 解决方案:
a.html:
<html>
<head>
<script src="jquery.js"></script>
<script>
$(function(){
$("#includedContent").load("b.html");
});
</script>
</head>
<body>
<div id="includedContent"></div>
</body>
</html>
b.html:
<p> This is my include file </p>
这将创建一个 DOM,其中主体部分如下所示:
<body>
<div id="includedContent">
<p> This is my include file </p>
</div>
</body>
但一个真实的#include
不包括包装div
,所以 DOM 看起来像:
<body>
<p> This is my include file </p>
</body>
知道如何在没有包装标签的情况下进行客户端包含吗?
编辑:我不想假设了解周围的标签。
例如,在这种情况下,周围的标签是body
,但并非在所有情况下都是如此。
我写的一个库的无耻插件解决了类似的问题。
https://github.com/LexmarkWeb/csi.js https://github.com/LexmarkWeb/csi.js
<div data-include="/path/to/include.html"></div>
以上内容将取/path/to/include.html
并替换div
用它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)