简体   繁体   English

如何在网页上同步导航栏更改

[英]How to have navigation bar changes sync across webpages

If I am making a website, what is the fastest way to put new pages onto the navigation bar, and be seen from every page. 如果我正在建立一个网站,那么将新页面放到导航栏上的最快方法是什么,并且可以从每个页面看到。 Each time a new page is created, the navigation bar is updated, and then I have to go copy and paste the new code onto every single page's code in my site (I think.) I have just now completed the homepage, so it isn't a problem yet, but I don't want to continue until I have this problem straightened out so I don't end up in trouble later. 每次创建新页面时,导航栏都会更新,然后我必须将新代码复制并粘贴到我站点中的每个页面代码上(我想。)我刚刚完成了主页,所以它不是这是一个问题,但我不想继续,直到我把这个问题理顺,所以我不会在以后遇到麻烦。 Is there some way to put the navigation bar's code onto a different HTML file and then link the pages to it so that they all stay synced up? 有没有办法将导航栏的代码放到不同的HTML文件上,然后将页面链接到它,以便它们都保持同步? Sorry if that is confusing, but please help me. 对不起,如果这令人困惑,但请帮助我。 Thank you so much, I am pretty new to HTML and CSS, but I am willing to learn whatever is required to optimize this. 非常感谢,我是HTML和CSS的新手,但我愿意学习优化这一点所需的一切。

Update: I now have this working using the PHP command: 更新:我现在使用PHP命令工作:

<?php include 'PHP/nav_bar_code.php'; ?>

The basic AJAX or XMLHTTPRequest call is (without resorting to libraries): 基本的AJAX或XMLHTTPRequest调用是(不依赖于库):

<script>
function loadNavigation() {
  var xmlhttp;
  if( window.XMLHttpRequest ) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else {// code for IE6, IE5
    xmlhttp=new ActiveXObject( "Microsoft.XMLHTTP" );
  }
  xmlhttp.onreadystatechange=function() {
    if( xmlhttp.readyState==4 && xmlhttp.status==200 ) {
      //--- change this to your navigation element
      document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
  //--- change the filename to the navigation file
  xmlhttp.open("GET","ajax_info.txt",true);
  xmlhttp.send();
}
</script>

When specifying the filename just be aware that the above will work in a flat file system and you may have to specify the full URL path to your navigation file is you are using a multi-level file structure - if that makes sense. 在指定文件名时,请注意以上内容将在平面文件系统中运行,并且您可能必须指定导航文件的完整URL路径,因为您使用的是多级文件结构 - 如果这有意义的话。

There a few methods to achieve this. 有几种方法可以实现这一目标。 An easy one is to use a CMS to create your site. 一个简单的方法是使用CMS来创建您的网站。 Something like WordPress, Joomla, Drupal, Plone, etc... The other approach could be to use AJAX as mentioned in the comments. 像WordPress,Joomla,Drupal,Plone等......其他方法可能是使用评论中提到的AJAX。 jQuery provides a simple method for this. jQuery为此提供了一种简单的方法。 All you would do is create a div at the head of each page and use the .load() method to load your header page in. It would probably be a better design to use ajax to load your pages into a framework. 你要做的只是在每个页面的头部创建一个div并使用.load()方法加载你的页眉。使用ajax将页面加载到框架中可能是一个更好的设计。 This is where you layout the main page, and set a content container, each of your subsequent pages are loaded into that container... 这是您布置主页面并设置内容容器的位置,您的每个后续页面都会加载到该容器中...

The other approach would be to use php files and the include command to include the header page. 另一种方法是使用php文件和include命令来包含头页面。 More info found here . 更多信息在这里找到。

If you're keen to get into AJAX, then this book is great - http://www.apress.com/9781590596678 如果你热衷于进入AJAX,那么这本书很棒 - http://www.apress.com/9781590596678

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

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