简体   繁体   English

动态导航与PHP包括和JavaScript

[英]dynamic nav with php include and javascript

I had a thought and wanted to pass it by my pears before I spend hrs coding this up. 我有一个想法,想在我花数小时编写代码之前先将其传递给我。

I have a php include file that contains the master nav. 我有一个包含主导航的php include文件。 The master nav is made up with ID'd div's 主导航由ID'd div组成

On each page the php is included has a javascript that runs a "getbyid" and collapse the div id that contains the page link that you are on and expands an already hidden div that contains a "active/highlighted" icon/text that indicates what page you are on. 在每个页面上都包含php,其中包含一个运行“ getbyid”的javascript,并折叠包含您所在页面链接的div id,并展开一个已隐藏的div,该div包含一个“活动/突出显示”的图标/文本,用于指示您所在的页面。

This way you have the joys of a master php nav, easy to edit and yet dynamic and user friendly. 这样,您就拥有了php nav大师的乐趣,易于编辑,但又动态且用户友好。

How does this method sound to anyone else? 这种方法对其他人有何影响?

As I am typing this I know this will not always work if JS is disabled or not installed. 当我输入此内容时,我知道如果禁用或未安装JS,此方法将永远无法使用。 Could this function be accomplished through php alone. 可以单独通过php完成此功能。

Thank you in advance for any ideas or additions you care to make. 预先感谢您提出的任何想法或补充。

What you are doing can be done in php. 您正在做什么,可以在php中完成。 You can get the URL of the page your are on using various server variables. 您可以使用各种服务器变量获取所在页面的URL。

$_SERVER['REQUEST_URI'] should be particularly helpful here. $ _SERVER ['REQUEST_URI']在这里应该特别有用。 But others may be helpful http://php.net/manual/en/reserved.variables.server.php 但其他人可能会有所帮助http://php.net/manual/zh/reserved.variables.server.php

Then, when you are rendering your nav include you can use that information and just add the appropriate css in the html you are echoing. 然后,在渲染导航包括时,您可以使用该信息,只需在要回显的html中添加适当的css。

Display:none for the div that should be collapsed and adding a class="active" to the div you want to be active. Display:none不应该折叠的div,并向要激活的div添加class =“ active”。

I believe this would be more reliable than javascript and your interface will be cleaner/smoother, without the page being edited post-load. 我相信这将比javascript更可靠,并且您的界面将更加整洁/流畅,而无需在加载后对页面进行编辑。

Get a couple of pages working in straight HTML and JS first, identify the bit that changes between those 2 pages, and then get PHP to output that change. 首先获取几个使用纯HTML和JS编写的页面,确定这两个页面之间发生更改的位,然后获取PHP以输出更改。

IF these pages are also expected to send GET or POST calls which PHP is supposed to act upon, then it is doubly important to make sure you have got code which basically works before bringing PHP into the mix. 如果还期望这些页面发送PHP应该执行的GET或POST调用,那么在将PHP引入混合之前,确保您的代码基本上可以正常工作就变得至关重要。

Also, to avoid other annoying errors make sure each ID on each page is unique. 另外,为避免其他令人讨厌的错误,请确保每个页面上的每个ID都是唯一的。

Hope these hints from one of your peers save you a bit of sanity. 希望您的同龄人的这些提示能为您节省一些理智。 :) :)

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

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