Table of Contents
Reply content:
Home Backend Development PHP Tutorial Why can't my page change color?

Why can't my page change color?

Aug 04, 2016 am 09:20 AM
php

This paging function outputs the page number

<code>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>';
       }</code>
Copy after login
Copy after login

This js code is used to change the color. Whichever one is clicked will turn red, while the others will change to the original color. But why every time I click, only the moment I click will turn red, and then it will change to the original color again. This js There is no problem with the code. I took out the js code separately and tested it. There is no problem. It can change color and other colors change to the original color. But why doesn’t it work here? It only turns red when clicked. Why is this? Is it because a jump occurred that it changed color and then changed back immediately? Why?

<code>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;
    }</code>
Copy after login
Copy after login

Reply content:

This paging function outputs the page number

<code>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>';
       }</code>
Copy after login
Copy after login

This js code is used to change the color. Whichever one is clicked will turn red, while the others will change to the original color. But why every time I click, only the moment I click will turn red, and then it will change to the original color again. This js There is no problem with the code. I took out the js code separately and tested it. There is no problem. It can change color and other colors change to the original color. But why doesn’t it work here? It only turns red when clicked. Why is this? Is it because a jump occurred that it changed color and then changed back immediately? Why?

<code>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;
    }</code>
Copy after login
Copy after login

Yes, because of the jump, each of your paging buttons will let you jump to it, and the jump means that your entire page will be reloaded once, and the styles and javascript will be re-read.

So you have to change your thinking: Since the jump cannot retain the effect of the previous web page, how do I know which page I am currently on?

It seems that your code is a mixture of php and HTML, so you can use php to determine which paging button is red

About using php to input html, there is a better way:

<? 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 ?>
Copy after login

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

Suppose there is a variable representing the current page: $currentPage

We can use a simple judgment to add a red background to the matching score page button

<? 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 ?>
Copy after login

Here’s the point:

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

If the current page and the page number of the paging button are consistent, return red, if not, return grey

Additional comments

It seems that you need to use a function to handle pagination, that is:

// 這邊我不太清楚 $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;
}
Copy after login

In this way, you can insert fenye(...)

where you need to output paginated content.
    <?php fenye($start, $end, $num, $page) ?>
Copy after login

Why can't my page change color?

Have you tried defining the style of the currently activated paging button and the style of the ordinary paging button in css

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles