将类添加到<a> Tag</a>

时间:2012-11-01 04:14:13

标签: php

这段代码我用来显示分页链接...问题是我需要在链接时添加一些样式才能激活它...我可以通过一些CSS规则来设置它...但是首先需要添加一个类来链接标签..例如:

    // Make the links to other pages, if necessary.
    if ($pages > 1) {

        echo '<div class="pagination">';            
            echo '<p>';

            // Determine what page the script is on:
            $current_page = ($start/$display) + 1;

            // If it's not the first page, make a Previous link:
            if ($current_page != 1) {
                echo '<a href="searching.php?s=' . ($start - $display) . '&p=' . $pages . '"><</a>';
            }

            // Make all the numbered pages:
            for ($i = 1; $i <= $pages; $i++) {
                if ($i != $current_page) {
                    echo '<a href="searching.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
                } else {
                    echo '<a class="current" href="#">' . $i . '</a> '; // Link to # so that it is not clickable.
                }
            } // End of FOR loop.           

            // If it's not the last page, make a Next button:
            if ($current_page != $pages) {
                echo '<a href="searching.php?s=' . ($start + $display) . '&p=' . $pages . '">></a>';
            }

            echo '</p>'; // Close the paragraph.

        echo '</div>';

    } // End of links section.  

任何人都知道如何做到这一点吗?

谢谢..

2 个答案:

答案 0 :(得分:2)

<a href="替换为<a class="current" href="

所以,你实际上需要这个CSS,因为我假设你一次会有一个current页面,加上普通的页面链接,以及上一个和下一个按钮:

<div class="pagination">
    <a href="#" class="prev">Previous</a>
    <a href="#">1</a>
    <a href="#" class="current">2</a>
    <a href="#">3</a>
    <a href="#" class="next">Next</a>
</div>

使用divpagination类进行换行,以便您可以更好地设置样式。现在对于样式,你可以使用这样的CSS:

.pagination {margin: 5px;} /* Give some margin */
.pagination a {color: #09f; text-decoration: none;} /* Give color, remove underline */
.pagination a:hover {font-weight: bold;} /* Effect when mouse over */
.pagination a.prev, .pagination a.next {color: #06f;} /* Let previous and next be in different color*/
.pagination a.current {color: #000; font-weight: bold;} /* Black and bold! */

截图:

这样看起来就像这样:

Screenshot

小提琴:http://jsfiddle.net/3Z9Jk/

要更改代码:

for ($i = 1; $i <= $pages; $i++) {
 if ($i != $current_page) {
  echo '<a href="searching.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
 } else {
  echo '<a class="current" href="#">' . $i . '</a> '; // Link to # so that it is not clickable.
 }
}

请更改这些:

1

if ($current_page != 1) {
    echo '<a class="prev" href="searching.php?s=' . ($start - $display) . '&p=' . $pages . '"><</a>';
}

2

if ($current_page != $pages) {
    echo '<a class="next" href="searching.php?s=' . ($start + $display) . '&p=' . $pages . '">></a>';
}

答案 1 :(得分:0)

您只需将class="whatever"添加到生成echo代码的相应<a>语句中。