【问题标题】:Load a specific div in the HTML file from other HTML file从其他 HTML 文件加载 HTML 文件中的特定 div
【发布时间】:2015-09-10 13:50:34
【问题描述】:

我有 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?这确实是我想实现的,但我仍然不知道怎么做。

【问题讨论】:

标签: javascript jquery html css


【解决方案1】:

既然你已经标记了jquery,我建议你使用 .loadjquery 中进行标记,如下所示:

您需要将控件传递给函数才能知道单击了哪个元素,如下所示:

<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"));
}

更新

不要为所有元素保留function,而是保留一个公共类并捕获event click

例如:

<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

好的,因为锚点将具有href 属性,它将直接执行发布并将整个页面加载到新页面中,因此您的load 将不会执行。因此,您要么需要阻止其默认操作,要么需要在单独的锚属性中提供 url 来获取,即 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
}

【讨论】:

  • 所有内容都已加载,即使不需要
  • 每个主播都有不同的展示内容
  • @GuruprasadRao 应该在click 处理程序中添加event.preventDefault() 吗?
  • @guest271314 没错……我也是这么想的。 Anywasy 提供了 2 个单独的演示来完成该功能。让 OP 试试吧.. :) 谢谢你的信息.. :)
  • 它不工作。当我点击菜单时,它保持在同一页面
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
  • 2020-09-17
  • 1970-01-01
  • 2016-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多