简体   繁体   English

使用php动态更改href值

[英]dynamically change href value using php

i have this problem that i need to change the directory address of href in anchor, i'm hoping to use php to make it work.. for example if i'm inside the home link my href value would be "index.php" but if im inside the folder of products link home href value will be change to "../index.php" because the header is just included to every page.. and also if you could suggest any alternatives for it, i would be happy to hear it.. thanks! 我有这个问题,我需要在锚中更改href的目录地址,我希望使用php使其起作用。例如,如果我在首页链接中,我的href值将是“ index.php”但是,如果im在产品文件夹的链接内部href值更改为“ ../index.php”,因为标题仅包含在每个页面中..而且,如果您可以提出其他建议,我会很高兴听到..谢谢!

<div class="header_link">
     <a href="../"><div class="links"><p>Home</p></div></a>
     <a href="products/"><div class="links"><p>Products</p></div></a>
     <div class="links"><p>Packages</p></div>
     <div class="links"><p>Price List</p></div>
</div>

You should use absolute paths instead of relative ones. 您应该使用绝对路径而不是相对路径。 Relative paths will become difficult to manage down the road, and as you've discovered they have to change depending on which page you're viewing. 相对路径将变得难以管理,而且正如您所发现的,它们必须根据正在查看的页面进行更改。

Instead of linking to ../index.php , just link to /index.php . 无需链接到../index.php ,只需链接到/index.php This will work regardless of which page you're on. 无论您在哪个页面上,它都将起作用。

Try using the base tag within the <head> of your html. 尝试在HTML的<head>中使用基本标记。

<base href="http://www.mysite.com">

or 要么

Make all href values absolute from the domain. 使所有href值都来自域绝对。

<a href="/index.php">Home</a>

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

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