我想将我的代码分开,所以我决定将每个代码分成div
html文件.每个HTML文件都有一些jQuery点击事件.我有2个文件index.html
和menu.html
.
问题是我必须在两个文件中包含jQuery库才能使它工作.
有没有什么方法可以让我一次包含库并使它适用于这两个文件?我试图仅在索引页面中包含库,但是单击菜单不起作用.
我menu.html
通过iframe 包含了该文件.
<iframe src="left-menu.html"></iframe>
Run Code Online (Sandbox Code Playgroud)
Boa*_*oaz 32
您可以使用jQuery 方法添加menu.html
到DOM中.这样创建的jQuery实例也将应用于从中加载的内容.index.html
load()
index.html
menu.html
例如,index.html
您可以这样做:
$(document).ready(function() {
$('#some-menu').load('some-local-path/menu.html');
});
Run Code Online (Sandbox Code Playgroud)
请注意我们如何加载内容的$('#some-menu')
另一个元素.同样重要的是要注意,由于同源策略,作为方法的一部分执行的AJAX请求仅适用于相同域,协议和端口中的文件(除非您使用CORS).index.html
menu.html
load()
在您的HTML中:
<body>
<header>
<div data-includeHTML="_HeaderPartial.html"></div>
</header>
<main>
<div>
This is the main content, from the index file
</div>
</main>
<footer>
<div data-includeHTML="_FooterPartial.html"></div>
</footer>
<script>
$(document).ready(function () {
$("div[data-includeHTML]").each(function () {
$(this).load($(this).attr("data-includeHTML"));
});
});
</script>
</body>
Run Code Online (Sandbox Code Playgroud)
在单独的文件“ _HeaderPartial.html”中:
<div>
<h1>This is the Header from its _Partial file</h1>
</div>
Run Code Online (Sandbox Code Playgroud)
在单独的文件“ _FooterPartial.html”中:
<strong>And this is from the footer partial file</strong>
Run Code Online (Sandbox Code Playgroud)
结果:
这是主要内容,来自索引文件
这是来自页脚部分文件
归档时间: |
|
查看次数: |
70464 次 |
最近记录: |