【问题标题】:Change hyperlink text color after page is rendered呈现页面后更改超链接文本颜色
【发布时间】:2018-01-29 15:33:42
【问题描述】:

我正在更改链接被选中后的颜色。颜色正在发生变化,但是当所选页面呈现时,它会返回到默认颜色。

php 页面如下所示:

<!DOCTYPE html>
<html>

<?php
    $Page = "Contact";

    include 'header.php';
?>

<body>

    <?php
        include "facebook.php";
    ?>

    <?php
        include "headers.php";
    ?>

    <?php
        include "navBar.php";
    ?>

    <?php
        include "containers.php";
    ?>

    <?php
        include "footer.php";
    ?>

</body>

</html>

进行选择的导航栏:

<div class="navBar">
        <a class="aNavBar" href="index.php">Home</a>     <a class="aNavBar" href="about.php">About</a>     <a class="aNavBar" href="galleries.php">Gallery</a>     <a class="aNavBar" href="equipment.php">Equipment</a>     <a class="aNavBar" href="links.php">Links</a>     <a class="aNavBar" href="contact.php">Contact</a>
    </div>

    <script>
       $(document).ready(function()
        {
            $('.navBar a').click(function()
            {
                var href = $(this).attr('href'); //location.href;
                alert(href);
                $(this).addClass('selected');
            });
        });
    </script>

我尝试将代码放入 main (contact).php 文件,但同样的事情正在发生。

【问题讨论】:

  • 这是预期的行为。页面加载之间不会保留 DOM 更改。如果要保存状态,则需要将信息存储在某处(服务器数据库、本地存储、会话、cookie 等),然后检索该状态并将其应用于您将来加载的每个页面。
  • 如果您只想突出显示链接到当前页面的菜单项,则无需在任何地方存储任何“附加”信息 - 该信息已包含在 $_SERVER 中。请去做一些研究,这并不是一个新话题。 google.com/search?q=php+menu+highlight+current
  • 一个简单的解决方案通常是将选定的菜单(被点击的菜单)传递给被点击的链接(作为获取变量),然后通过 PHP 在该菜单项上设置一个 CSS 类。

标签: php jquery html hyperlink


【解决方案1】:

您也可以通过以下方式进行操作

$path = $_SERVER['PHP_SELF']; // will return http://test.com/index.php for our example
$page = basename($path); // will return index.php

然后放条件

<a class=<?php ($page == index.php) ?  echo "aNavBar selected" : echo "aNavBar"; ?> href="index.php">Home</a>

【讨论】:

  • 以上代码应该在navBar.php文件中吗?我试过了,还是不行。
  • 在 navBar.php 中是的,请删除 jquery 代码以在此处添加类。并且还请检查您的$page 并且您与页面返回相同的值进行比较
【解决方案2】:

在 html 中定义的链接具有标准颜色,即使它们是否被点击。定义是否单击链接的是浏览器缓存。当您验证它是否被选中时,当页面加载时,此信息将不会被保留。将此链接的点击保存在用户历史记录中的数据库中可能会很有趣,因为通过浏览器是不可能的。

【讨论】:

    【解决方案3】:

    简单地说,页面状态不会在页面重新加载或更改后保持不变。因此,您不应期望在加载第二页时选定的超链接保持“选定”状态。不过,您可以通过编程方式完成:

    类似这样的:

    <div class="navBar">
        <a class="aNavBar<?php if(strpos($_SERVER['PHP_SELF'], 'index.php')!==false) echo ' selected'; ?>" href="index.php">Home</a>     <a class="aNavBar" href="about.php<?php if(strpos($_SERVER['PHP_SELF'], 'about.php')!==false) echo ' selected'; ?>">About</a>
    

    【讨论】:

      【解决方案4】:

      以下工作:

        <?
                  $path = $_SERVER['PHP_SELF']; // will return http://test.com/index.php for our example
                  $page = basename($path); // will return index.php
              ?>
              <a class="aNavBar" <? if($page == "index.php") echo "style='color:red'";?> href="index.php">Home</a>  
      

      感谢您的帮助!

      【讨论】:

        猜你喜欢
        • 2010-11-18
        • 2015-11-08
        • 2016-11-10
        • 2013-05-12
        • 2016-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-01
        相关资源
        最近更新 更多