gpt4 book ai didi

JavaScript addClass 不适用于 PHP GET ['page']

转载 作者:行者123 更新时间:2023-11-30 12:38:23 24 4
gpt4 key购买 nike

我一直在尝试实现以下 javascript 以将最后单击的列表项设置为“事件”类,并在适用的情况下从其他列表项中删除该类...

<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 编写(并且已经编写过)相同的功能,但是,我想了解为什么这段代码不起作用,有人有任何想法吗?

最佳答案

在页面导航时附加点击事件将不允许 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>

关于JavaScript addClass 不适用于 PHP GET ['page'],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25345693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com