php - 为什么我这个分页变不了颜色呢?

查看:86
本文介绍了php - 为什么我这个分页变不了颜色呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

这个分页函数输出页码

for($i=$start;$i<=$end;$i++){
        echo '<li style="width:20px;display:inline-block;
        height:25px;border:1px solid black;line-height:25px">
        <a class="pages" style="display:inline-block;width:100%;height:100%;" href="'.$_SERVER["SCRIPT_NAME"].'?page='.$i.'&num='.$num.'">'.($i).'</a>
        </li>';
       }

这个js代码用来改变颜色  点击哪个哪个就变成红色  同时其它的变成原来的颜色     可是为什么我每次点击是只有点击的一瞬间变成红色呢 然后就又变成了原来的颜色   这个js代码没有问题  我把js代码单独拿出去测试没有问题 可以变色同时其它的变成原来的颜色    可是为什么在这里却不行呢 只有点击的一瞬间变成红色  这是为什么?是不是因为发生了跳转所以变了一下色又马上变回去了?到底是为什么?

var topMenus = getClass('a','pages');
       for(var i=0;i < topMenus.length; i++)
       { 
         topMenus[i].onclick=function(){
          for(var i=0;i < topMenus.length; i++){
            topMenus[i].style.backgroundColor="#858585"
          }  
          this.style.backgroundColor="red";  
         }
       }
 
     function getClass(tagName,className) 
{
    if(document.getElementsByClassName) 
    {        return document.getElementsByClassName(className);
    }
    else
    {       var tags=document.getElementsByTagName(tagName);
        var tagArr=[];
        for(var i=0;i < tags.length; i++)
        {
            if(tags[i].class == className)
            {
                tagArr[tagArr.length] = tags[i];
            }
        }
        return tagArr;
    }

解决方案

沒錯,就是因為跳轉,你的每個分頁按鈕都會讓你跳轉到,而跳轉就意味著你整個頁面會重載一次,樣式、javascript 都會重新讀取。

所以你得轉換思路:既然跳轉不能保留上一個網頁的效果,那我該如何知道當前是哪一個頁面?

看你代碼是 phpHTML 混合,所以可以用 php 判定現在頁面的方式來決定哪個分頁按鈕為紅色

關於利用 php 輸入 html ,有個方式更好:

<? for( $i = $start ; $i <= $end ; $i++): ?>
    <li style="width:20px;display:inline-block;height:25px;border:1px solid black;line-height:25px">
        <a class="pages" style="display:inline-block;width:100%;height:100%;" href="<?= $_SERVER["SCRIPT_NAME"] . '?page=' . $i . '&num=' . $num ?>"><?= $i ?></a>
    </li>
<? endfor ?>

<?= $something?> = <? echo $something ?>

那假設現在有一個變量代表著當前頁: $currentPage

我們就可以用一個簡單的判斷式來為符合得分頁按鈕加上紅色背景

<? for( $i = $start ; $i <= $end ; $i++): ?>
    <li style="width:20px;display:inline-block;height:25px;border:1px solid black;line-height:25px">
        <a class="pages" style="background:<?= $currentPage === $i ? 'red' : 'grey' ?>;display:inline-block;width:100%;height:100%;" href="<?= $_SERVER["SCRIPT_NAME"] . '?page=' . $i . '&num=' . $num ?>"><?= $i ?></a>
    </li>
<? endfor ?>

重點在這裡:

background:<?= $currentPage === $i ? 'red' : 'grey' ?>

如果當前頁和分頁按鈕的頁數一致,就返回 red , 不符合就返回 grey

補充評論

看起來你是要用一個函數來處理分頁,那就是:

// 這邊我不太清楚 $num 是什麼
function fenye($start, $end, $num, $current) {
    $html = '<ul>';
    for( $i = $start ; $i <= $end ; $i++) {
        $background = $i === $current ? 'red' : 'grey';
        $html.= '<li style="width:20px;display:inline-block;height:25px;border:1px solid black;line-height:25px">';        
        $html.= "<a class=\"pages\" style=\"background: $background;display:inline-block;width:100%;height:100%;\" href=\"$_SERVER[SCRIPT_NAME]?page=$i&num=$num\">$i</a>";    
        $html.= '</li>';
    }
    $html.= '</ul>';
    echo $html;
}

這樣你在需要輸出分頁內容的地方插入 fenye(...)


    <?php fenye($start, $end, $num, $page) ?>

这篇关于php - 为什么我这个分页变不了颜色呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆