在呈现页面后更改超链接文本颜色

时间:2017-08-21 14:04:59

标签: php jquery html hyperlink

选择后,我正在更改链接的颜色。颜色正在变化,但是当渲染所选页面时,它将返回默认颜色。

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文件中,但同样的事情正在发生。

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您也可以按照以下方式执行此操作

$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>

答案 2 :(得分:0)

简单地说,页面重新加载或更改后页面状态不会保留。因此,在加载第二页时,您不应期望所选超链接保持“已选中”。你可以编程地执行它:

这样的事情:

<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>

答案 3 :(得分:0)

以下工作:

  <?
            $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>  

感谢您的帮助!