Disable link to the current page in navigation

亡梦爱人 提交于 2020-01-30 05:24:34

问题


I have a pretty standard navigation for a website. The main navigation is a ul, with several nested ul's. I'm basically just wanting to dynamically disable a link when the user is already on that page.

So if I'm on the home page, the home page link needs to be disabled. If I'm on the contact page, the contact page link needs to be disabled.

I am not wanting to delete the link entirely, just remove the href from the anchor or just make the anchor itself de-activated. Is there a fast, slick and elegant solution for this in XHTML, CSS, JavaScript or php. I'd also want a solution that's friendly across the board, not just for modern browsers (you can read, IE 6).

I did think of setting a path variable on each page, and then having an if statement for each link, but that seems horribly tedious and stupid. But hey, it would get the job done.


回答1:


I think it would be best to do this in the page generation code (i.e. PHP), iterating through each link and removing the <a> tag, replacing it with a <span> or something similar if the link matches the criteria for matching the current page. PHP gives you this current page (in various flavors: $_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_FILENAME'], $_SERVER['PHP_SELF'], $_SERVER['QUERY_STRING'], etc.) in its base, and using these, you can determine an algorithm to match your page.



来源:https://stackoverflow.com/questions/3672498/disable-link-to-the-current-page-in-navigation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!