简体   繁体   English

如果我有一段HTML在多个html文件中通用,那么我可以在自己的.html文件中拥有它吗?

[英]If I have a section of HTML that is common across several html files, can I have it in its own .html file?

The title might have been a bit confusing, sorry about that. 标题可能有点令人困惑,对此感到抱歉。 Let me clarify. 让我澄清一下。

So in my website, I have a <nav> section which is present on every .html page for the site. 因此,在我的网站中,该网站的每个.html页面上都有一个<nav>部分。 If I want to make a change to the nav menu, I have to change it on every single page. 如果要更改导航菜单,则必须在每个页面上进行更改。

Is there a way that I can have that <nav> section in its own .html file (eg nav.html) and then have it produced on every site .html page? 有没有办法可以在自己的.html文件(例如nav.html)中包含该<nav>部分,然后在每个站点的.html页面上生成它? That way, if I need to make a change then it only needs to be made in a single file (ie nav.html) and will still update on all pages when it's run. 这样,如果我需要进行更改,则只需在单个文件(即nav.html)中进行更改,并且在运行时仍将在所有页面上进行更新。

If I can do that somehow, how can I make it so that it will scroll to the top of the page if the nav button of the current page is pressed? 如果我以某种方式做到这一点,那么如果按下当前页面的导航按钮,如何使其滚动到页面顶部?

So if I'm on photos.html and I click the "Photos" link on the nav bar (which would usually take me to photos.html), can I make it not refresh the page but instead just go to top, for example? 因此,如果我在photos.html上,并且单击导航栏上的“照片”链接(通常会将我带到photos.html),我可以使其不刷新页面而是直接转到顶部吗? ? (ie <a href="#">Photos</a> ) (即<a href="#">Photos</a>

Is there a way that I can have that section in its own .html file (eg nav.html) and then have it produced on every site .html page? 有没有一种方法可以让我将该节放在自己的.html文件中(例如nav.html),然后在每个网站的.html页面上生成它?

You can use .load() or $.get() with .append() or .prepend() . 您可以将.load()$.get().append().prepend()

$(function() {
  $.get("nav.html")
  .then(function(html) {
    $("body").prepend(html);
    $("a[href='#']:contains(Photos)").on("click", function(e) {
      e.preventDefault();
      $("html, body").animate({
        scrollTop:0
      }, 1000);
    });
  });
});

In order to have the Navbar in only one file jquery's .load or .get functions would be the easiest solution as stated in guest271314 answer. 为了使Navbar仅存在于一个文件中,jquery的.load.get函数将是guest271314答案中所述的最简单的解决方案。 Although I would do something more like this to load the content: 尽管我会做更多类似的事情来加载内容:

$(document).ready(function() {
    $('#nav-container').load('nav.html');
});

I realize this would mean you would need <div id="nav-container"></div> on each page but this seems like a cleaner solution to me. 我意识到这意味着您将需要在每个页面上<div id="nav-container"></div> ,但这对我来说似乎是一个更干净的解决方案。

For the page jump you can simply use an id in your href like so. 对于页面跳转,您可以像这样简单地在href中使用id This will make it so the page will scroll to the top of the div with the id you enter into the href . 这样可以使页面滚动到div的顶部,其中包含您在href输入的id

<div id="pictures"></div>
<a href="#pictures">Pictures</a>

You can also add some jQuery to make the page jump smooth. 您还可以添加一些jQuery以使页面平滑跳转。 The script below was taken from this CSS-Tricks article and will make that page jump smooth. 下面的脚本来自此CSS-Tricks文章,它将使该页面平滑跳转。

$(function() {
    $('a[href*="#"]:not([href="#"])').click(function() {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
  var target = $(this.hash);
  target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
            if (target.length) {
                $('html, body').animate({
                    scrollTop: target.offset().top
                }, 1000);
                return false;
            }
        }
    });
});

I would create an include file called something like navMenu.php that just has your nav menu code in it. 我会创建一个名为navMenu.php之类的包含文件,其中仅包含您的导航菜单代码。

<nav> menu code here </nav>

Then each of your pages that you want to include this code rename with the extension .php 然后,您要包含此代码的每个页面都使用扩展名.php重命名。
Then wherever in these other pages you want to include this nav menu code write 然后,在这些其他页面中的任何位置,您都希望包括此导航菜单代码

<?php require 'navMenu.php' ?>

Refer to: http://php.net/manual/en/function.require.php 请参阅: http : //php.net/manual/en/function.require.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM