JavaScript的addClass不工作与PHP GET['page']

JavaScript addClass not working with PHP GET['page']

本文关键字:page GET PHP addClass 工作 JavaScript      更新时间:2023-09-26

我一直在尝试实现以下javascript设置最后点击的列表项与'active'类,并从其他列表项中删除类,如果适用…

<script>
            $(function() {
                $('ul li').click( function() {
                    $(this).addClass('active').siblings().removeClass('active');
                });
            });
        </script>

这段代码在静态html页面上工作,但当包含以下代码时,我似乎无法让它工作…

<!DOCTYPE html>
<html lang="en">
    <head>  
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
        <style type="text/css">
            p{
                font-size: 32px;
                font-weight: bold;
                text-align: center;
            }           
            #banner 
            {
                width: 100%;
            }
        </style>
        <script>
            $(function() {
                $('ul li').click( function() {
                    $(this).addClass('active').siblings().removeClass('active');
                });
            });
        </script>
    </head>
    <body>
        <div class="container">
            <img id="banner" src="Banner.png" />
        </div>
        <div class="container">
             <nav role="navigation" class="navbar navbar-inverse">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-toggle">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a href="#" class="navbar-brand"></a>
                </div>
                <!-- Collection of nav links, forms, and other content for toggling -->
                <div id="navbarCollapse" class="collapse navbar-collapse">
                    <ul class="nav navbar-nav">
                        <li><a href="index.php?page=home">Home</a></li>
                        <li><a href="index.php?page=music">Music</a></li>
                        <li><a href="index.php?page=videos">Videos</a></li>
                    </ul>
                    <ul class="nav navbar-nav navbar-right">
                        <li><a href="#">Login</a></li>
                    </ul>
                </div>
            </nav>  
        </div>
        <?php
            if (isset($_GET['page'])) 
            {
                // include selected page into the current screen
                switch ($_GET['page']) 
                {
                    case 'home':
                        include("home.html");
                        break;
                    case 'music':
                        include("music.html");
                        break;
                    case 'videos':
                        include("videos.html");
                        break;
                    default:
                        include('home.html');
                }
            } 
            else 
            { // If no button has been selected, then display the default page
                include("home.html");
            }   
        ?>    
    </body>
</html>

我可以使用PHP编写(并且已经编写)相同的功能,但是,我想了解为什么这段代码不起作用,有人有任何想法吗?

在页面导航时附加一个click事件将不允许jQuery执行。

相反,当php执行时,在列表项上添加一个活动类。

<li class="<?php echo ($_GET['page']=='home')?'active':'';?>"> </li>


用下面的代码替换菜单

<ul class="nav navbar-nav">
      <li class="<?php echo ($_GET['page']=='home')?'active':'';?>"><a href="index.php?page=home">Home</a></li>
      <li class="<?php echo ($_GET['page']=='music')?'active':'';?>"><a href="index.php?page=music">Music</a></li>
      <li class="<?php echo ($_GET['page']=='videos')?'active':'';?>"><a href="index.php?page=videos">Videos</a></li>
</ul>

使用window.load

$(window).load(function(){  
        $('ul li').click( function() {
            $(this).addClass('active').siblings().removeClass('active');    
        });
});

或者如果你使用jquery,使用document.ready

$(document).ready(function() 
{
    $('ul li').click( function() {
        $(this).addClass('active').siblings().removeClass('active');
    });
});