我有 2 页 html。一页仅包含所有菜单,这是我的 index.html。第二个 html 文件或 home.html 包含所有菜单的所有数据。我的意思是,来自不同菜单的所有数据都在此页面中。
我想要的是,一旦我单击 index.html 中的特定菜单,只有该菜单的内容会显示在它在 home.html 中的屏幕上。
这是我在 index.html 中的代码
<div class="list-group topics">
<a href="home.html #headrest" onclick="headrest_main()" class="list-group-item list-group-item-success sub_topic"><i class="fa fa-circle-o text-red"></i> <span>Headrest Guide Clearance</span></a>
</div>
主页.html
<aside class="main-sidebar">
<!-- sidebar: style can be found in sidebar.less -->
<section class="sidebar">
<!-- /.search form -->
<!-- sidebar menu: : style can be found in sidebar.less -->
<ul class="sidebar-menu">
<!-- <li class="header">MAIN NAVIGATION</li> -->
<li><a href="#headrest" onclick="headrest()"><i class="fa fa-circle-o text-aqua"></i> <span>Headrest Guide Clearance</span></a></li>
<!-- <li class="header">LABELS</li> -->
<li><a href="#structures" onclick="structures()"><i class="fa fa-circle-o text-red"></i> <span>Structures</span></a></li>
<li><a href="#foams"><i class="fa fa-circle-o text-yellow"></i> <span>Foams</span></a></li>
<li><a href="#fasteners"><i class="fa fa-circle-o text-teal"></i> <span>Fasteners</span></a></li>
<li><a href="#recliner"><i class="fa fa-circle-o text-olive"></i> <span>Recliner Mechanism</span></a></li>
<li><a href="#plastics"><i class="fa fa-circle-o text-teal"></i> <span>Plastics</span></a></li>
<li><a href="#trims"><i class="fa fa-circle-o text-olive"></i> <span>Trims</span></a></li>
<li><a href="#seat"><i class="fa fa-circle-o text-olive"></i> <span>Seat Tracks</span></a></li>
</ul>
</section>
<!-- /.sidebar -->
</aside>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header hide">
<h1>
Dashboard
<small>Control panel</small>
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
<li class="active">Dashboard</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
<!-- Main row -->
<div class="row">
<div id="headrest">
<h1 class="title_topic">Headrest Guide Clearance</h1>
</div>
</div><!-- /.row (main row) -->
<div class="row">
<div id="structures">
<h1 class="title_topic">Structures</h1>
</div>
</div><!-- /.row (main row) -->
</section><!-- /.content -->
</div><!-- /.content-wrapper -->
Javascript:
function headrest_main() {
document.getElementById("headrest").innerHTML='<object type="text/html" data="home.html"></object>';
}
但是这里的这段代码仍然不起作用。有没有一种方法可以加载 HTML 文件,但只能加载特定的 div?这真的是我想实现的,但我仍然不知道该怎么做。
最佳答案
因为你已经标记了 jquery
我建议你在 jquery
中使用 .load 如下:
您需要将控件传递给函数以了解单击了哪个元素,如下所示:
<div class="list-group topics">
<a href="home.html #headrest" onclick="headrest_main(this)" class="list-group-item list-group-item-success sub_topic"><i class="fa fa-circle-o text-red"></i> <span>Headrest Guide Clearance</span></a>
^^^^
</div>
然后使用.load
function headrest_main(ctrl) {
$("#headrest").load($(ctrl).attr("href"));
}
更新
不是为所有元素保留函数
,而是保留一个公共(public)类并捕获事件点击
:
例如:
<div class="list-group topics">
<a href="home.html #headrest" class="list-group-item list-group-item-success sub_topic"><i class="fa fa-circle-o text-red"></i> <span>Headrest Guide Clearance</span></a>
</div>
假设 sub_topic
是所有 anchors
的通用类,您可以如下捕获 event
而不是添加 onclick
$(".sub_topic").on('click',function(){
$("#headrest").load($(this).attr("href"));
});
更新 2
好的,因为 anchor 将具有 href
属性,它将直接执行发布并将整个页面加载到新页面中,因此您的 load
将不会执行。因此,要么您需要阻止其默认操作,要么您需要提供 url
以在单独的 anchor 属性中获取,即 home.html #headrest
:
解决方案 1 - 防止默认操作
我正在使用我的第二种捕获点击事件的方法
$(".sub_topic").on('click',function(e){
e.preventDefault();//this will prevent the default action of anchor
$("#headrest").load($(this).attr("href"));
});
解决方案 2 - 存储在不同的属性中
更改 href="#"
并将实际的 href
存储在不同的 attribute
<div class="list-group topics">
<a href="#" data-href="home.html #headrest" onclick="headrest_main(this)" class="list-group-item list-group-item-success sub_topic"><i class="fa fa-circle-o text-red"></i> <span>Headrest Guide Clearance</span></a>
<!-- ^^^^ store url to load in some data-* attribute -->
</div>
和JS
function headrest_main(ctrl) {
$("#headrest").load($(ctrl).attr("data-href")); //get the value from data-href
}
关于javascript - 从其他 HTML 文件加载 HTML 文件中的特定 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32493585/