Html 在多个页面上有一个菜单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2216347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:08:22  来源:igfitidea点击:

Have a single menu on multiple pages?

htmlmenu

提问by Bob Dylan

So I have a website and I'm using a basic menu located at the top right corner of the screen. It's links are as follows:

所以我有一个网站,我使用的是位于屏幕右上角的基本菜单。它的链接如下:

| Home | Blog | Results | Pictures | Our Location |

Form time to time I need to add a new link to the menu or change where one of the links points to. This means that on every page that the menu exist I must manually change the link. Surely there is some way to have a master menu that is just put on every page.

我不时需要向菜单添加一个新链接或更改其中一个链接指向的位置。这意味着在菜单存在的每个页面上,我必须手动更改链接。当然,有一些方法可以让主菜单放在每一页上。

Or am I dreaming?

还是我在做梦?

采纳答案by Max Shawabkeh

Put the menu in a separate file and include it on the server side, either using a full-blown scripting language like PHP (one line) or using SSI.

将菜单放在一个单独的文件中并将其包含在服务器端,要么使用成熟的脚本语言,如 PHP(一行),要么使用SSI

回答by Matthew Flaschen

There are various ways to do this. It depends what you have access to on the server. Possibly the simplest mechanism is server-side includes. You would just have a file that contains the menu and include it on every page.

有多种方法可以做到这一点。这取决于您在服务器上可以访问的内容。可能最简单的机制是服务器端包含。您将只有一个包含菜单并将其包含在每个页面上的文件。

You can also do this with every programming language in more or less elegant ways.

您也可以通过或多或少优雅的方式对每种编程语言执行此操作。

EDIT: The SSI is quite simple. You can just make a /header.html file, then do:

编辑:SSI 非常简单。您可以制作一个 /header.html 文件,然后执行以下操作:

 <!--#include virtual="/header.html" --> 

in the appropriate place.

在适当的地方。

回答by pixeltocode

use the PHP include on all your pages

在所有页面上使用 PHP 包含

<?php include 'includes/menu.php'; ?>

and have a separate menu.php within a folder named 'includes'. you'll need to save all oyur pages as .php

并在名为“includes”的文件夹中有一个单独的 menu.php。您需要将所有 oyur 页面保存为 .php

you can make your footer as an include too

您也可以将页脚设为包含

回答by Ahmed Mansour

I think this is the easiest way of doing it without a server side language. Use a javascript library like W3Data. All you need to do is add the w3-include-html attribute to your div. Done!

我认为这是在没有服务器端语言的情况下最简单的方法。使用像W3Data这样的 javascript 库。您需要做的就是将 w3-include-html 属性添加到您的 div 中。完毕!

If your menu is in a file called menu.html you'd do something like this.

如果您的菜单在一个名为 menu.html 的文件中,您将执行类似的操作。

<div w3-include-html="menu.html"></div> 

回答by Sinan ünür

You can generate pages from templates before uploading them: See Template Toolkit.

您可以在上传之前从模板生成页面:请参阅模板工具包

回答by H. Green

Have a server side program determine what menus and locations of the links need to be there. Then use ajax to periodically poll the server side script to get the contents of the menu at the current time.

让服务器端程序确定哪些菜单和链接的位置需要在那里。然后使用ajax定期轮询服务端脚本,获取当前时间菜单的内容。

It'd be easy to have it send an xml data back like:

让它发送一个 xml 数据很容易,例如:

<menu>
    <link name="Home" href="destinationhome.html"/>
...
    <link name="Pictures" href="....html"/>
 </menu>

Then build your links from that data.

然后从该数据构建您的链接。