PHP - 突出显示所选链接

时间:2009-12-12 01:13:56

标签: php css hyperlink highlighting

如何“突出显示”(转向不同颜色,制作粗体,等等......)已点击的链接?

示例在这里:http://www.celebrything.com/ 尝试获取右侧边栏中的“今天”,“周”和“月”链接,以便在单击后变为不同的颜色。

以下是我用于在右侧边栏中显示结果的代码:

<div id="sidebar">

<div class="post">
<h2>

<font color="#333333">Top 50 Celebrities</font>
<br>
<br>
<font color="#333333"><a href="index.php?table=today">Today</a></font>
<font color="#333333"><a href="index.php?table=week">Week</a></font>
<font color="#333333"><a href="index.php?table=month">Month</a></font>
</font>
<br>
<br>

<?php

function showTable ($table){

if (!in_array($table, array('today', 'week', 'month'))) {
  return false;
}

global $wpdb;
$result = $wpdb->get_results('SELECT name, count FROM wp_celebcount_' . $table);
foreach($result as $row) {
echo '<a href="http://www.celebrything.com/?s=' .
    urlencode($row->name) . '&search=Search">' . $row->name .
    '</a> - ' . $row->count . ' Posts<br/>';
}
}


if (!empty($_GET['table'])) {
showTable($_GET['table']);

} else { showTable('today'); }

?>




</h2>
</div>

</div>

<div class="clear"></div>

2 个答案:

答案 0 :(得分:5)

CSS可以做到这一点。

如果在任何时候访问过某个链接:

<style type="text/css">
a:visited { color: red; }
</style>

如果链接具有焦点:

a:focus { color: red; }

注意: IE7及更低版本不支持:focus。请参阅CSS contents and browser compatibility:focus

答案 1 :(得分:1)

如果您询问如何使当前页面处于活动状态,那么您将如何操作:

<font color="#333333"><a class="<?php echo currentPage('today') ?>" href="index.php?table=today">Today</a></font>
<font color="#333333"><a class="<?php echo currentPage('week') ?>" href="index.php?table=week">Week</a></font>
<font color="#333333"><a class="<?php echo currentPage('month') ?>"href="index.php?table=month">Month</a></font>


function currentPage($isTableSet)
{
    if($_GET['table'] == $isTableSet)
        return 'selected'
    else
        return '';
}

你需要在你的CSS中添加.selected类并将其设置为你想要的任何类型,可能是这样的:

<style type="text/css">
    .selected  {
        font-weight: bold;
    }
</style>