【问题标题】:Hide link when on specified page在指定页面时隐藏链接
【发布时间】:2016-05-27 07:11:22
【问题描述】:

我有一个带有联系我们链接和版权信息的页脚文件。当我单击联系我们链接时,它将转到联系我们页面。我的页脚是一个 php 文件,只包含在相应的页面中。我想在联系我们页面中隐藏联系我们链接。我该怎么做?

footer.php

<?php
    echo '<footer class="footer">
              <div class="container">
                  <p class="text-muted"><a href="contactus.html">Contact Us</a></p>
                  <p class="text-muted"> Copyright &copy; <span id="yearfooter"> </span>. All rights reserved.</p>
              </div>
          </footer>';
?>

【问题讨论】:

  • $_SERVER['REQUEST_URI'] 将返回当前页面 URL
  • 为什么要回显页脚而不是显示为 html 标记并在包含的页面上回显?如果相应地进行更改,您可以使用 javascript 或 jquery 来完成此操作
  • @theinarasu 我还没有尝试过。在呼叫者处回声?
  • basename(__FILE__) 为您提供当前文件名。

标签: javascript php html css


【解决方案1】:

这将起作用:

$page_name=preg_replace('#^(.+[\\\/])*([^\\\/]+)$#', '$2', $_SERVER['PHP_SELF']);
if ($page_name!="about-us.html") {
    //display your link
}

【讨论】:

    【解决方案2】:

    你可以做这样的事情。不要使用echo,只需在&lt;?php ?&gt;标签之外编写html并检查$_SERVER['REQUEST_URI'],如果不是联系页面,则仅显示链接。

    <footer class="footer">
        <div class="container">
            <?php if (strpos($_SERVER['REQUEST_URI'], '/contactus.html') !== 0) { ?>
            <p class="text-muted"><a href="contactus.html">Contact Us</a></p>
            <?php } ?>
            <p class="text-muted"> Copyright &copy; <span id="yearfooter"> </span>. All rights reserved.</p>
        </div>
    </footer>
    

    【讨论】:

    • 谢谢!我试过了,联系我们页面链接仍然显示在联系我们页面中。
    • @MichelleAshwini 嗯,contactus 页面的 uri 到底是什么?我的意思是域之后的所有内容(.com.net 等)。
    • @Andrew 如果像 example.com/some-dir/yourpage.php?q=bogus&n=10 和 $_SERVER['REQUEST_URI'] 这样的网址会给你 -> /some-dir/yourpage.php?q=bogus&n=10
    • @theinarasu 我知道,但我认为contactus.html 页面不会附加任何查询字符串。我想他们可以检查生成它的文件,但它可能是所有内容的一个 php 文件。
    • 这就是为什么你应该像我在回答中提到的那样解析链接:$page_name=preg_replace('#^(.+[\\\/])*([^\\\/]+)$#', '$2', $_SERVER['PHP_SELF']);
    【解决方案3】:

    你可以试试这个..

    <footer class="footer">
            <div class="container">
                <p class="text-muted"><a id="a" href="contactus.html">Contact Us</a></p>
                <p class="text-muted"> Copyright &copy; <span id="yearfooter"> </span>. All rights reserved.</p>
            </div>
        </footer>
    

    在javascript中

         var pathname = window.location.pathname;
            var appDomainEndding = 'yourdomain.com/app/'
            if (pathname.toLowerCase().indexOf("contactus.html") > -1 || 
                pathname.indexOf(appDomainEndding, pathname.length - appDomainEndding.length) > -1)
           // add class to hide the a tag 
    {
    document.getElementById("a").style.visibility = "hidden";
    }
    

    【讨论】:

    • 谢谢!它仍然显示联系我们的链接。
    • @MichelleAshwini 你改变了 appDomainEndding 的值吗?
    • 由于我目前在本地服务器上部署它,所以我把它作为 localhost/website/。这是我的源文件所在的位置
    • 你可以删除这个 pathname.indexOf(appDomainEndding, pathname.length - appDomainEndding.length) > -1 然后再试一次
    • @MichelleAshwini 我更新了我的答案.. 再试一次
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 2013-01-25
    • 2016-12-06
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多