简体   繁体   English

HTML包含带有相对链接的导航元素

[英]HTML include navigation elements with relative links

I included a navigation element in multiple pages like this: <script> $(function(){ $("#includedNavigation").load("navigation.html"); }); </script> 我在多个页面中都包含了一个导航元素,如下所示: <script> $(function(){ $("#includedNavigation").load("navigation.html"); }); </script> <script> $(function(){ $("#includedNavigation").load("navigation.html"); }); </script>

But this works just for html-files in the same directory "subdir". 但这仅适用于同一目录“ subdir”中的html文件。 The navigation.html cannot be reached from the index.html in the upper main directory "dir". 从上部主目录“ dir”中的index.html无法访问navigation.html。 If I add the upper pattern to the index.html in the directory above the navigation.html is reached correctly but the relative links do not work anymore. 如果我将上部模式添加到navigation.html上方目录中的index.html中,则可以正确访问,但相对链接不再起作用。 I don't want to replace the relative links by absolute paths. 我不想用绝对路径替换相对链接。 Is it possible to switch between different links in navigation.html depending on wherefrom it is called? 是否可以根据其调用来源在navigation.html不同链接之间切换? Any other ideas? 还有其他想法吗? Thanks a lot! 非常感谢!

草案

Since this is a multiple page website that reuses the same menu on all its pages, there's no functional or logical difference between page1.html and index.html apart from index probably being the first page you see. 由于这是一个多页网站,在其所有页面上都重复使用相同的菜单,因此page1.htmlindex.html之间的功能或逻辑上没有区别,除了索引可能是您看到的第一页。 So I would just put index.html in the same folder and call it a day. 因此,我只需要将index.html放在同一文件夹中,然后命名为一天即可。 It would make sense to have it on an upper level if the index page would load all the other pages into itself. 如果索引页会将所有其他页面加载到自身中,则将其设置为较高级别是有意义的。 Then the menu would only need to be included on the index page. 然后,该菜单仅需要包含在索引页面上。

A folder structure is a project requirement, not a technical requirement. 文件夹结构是项目要求,而不是技术要求。 In development, all pages are likewise divided into sub directories and such to organize the files. 在开发中,所有页面都同样分为子目录,以便组织文件。 But with running the deployment script to copy everything into production, the deploy script concatenates everything into one file anyway and the entire folder structure disappear. 但是通过运行部署脚本将所有内容复制到生产环境中,部署脚本无论如何将所有内容连接到一个文件中,整个文件夹结构消失了。

It's perfectly normal to have a clear folder structure for development organised by business needs and also have a completely different folder structure for live code organised by technical needs. 拥有清晰的文件夹结构用于按业务需求组织的开发是完全正常的,并且对于按技术需求组织的实时代码也具有完全不同的文件夹结构。

you have to give the path of navigation.html file. 您必须提供navigation.html文件的路径。

$(function(){ $("#includedNavigation").load("./dir/navigation.html"); }); $(function(){$(“#includedNavigation”)。load(“ ./ dir / navigation.html”);});

like this 像这样

Can you use something like this 你能用这样的东西吗

<link rel="import" href="navigation.html">

Or 要么

$(function(){ $("#includedNavigation").load("path/to/navigation.html"); });

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

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