JavaScript addClass不使用PHP GET[';页面';]
我一直在尝试实现以下javascript,将最后单击的列表项设置为“active”类,并从其他列表项中删除该类(如果适用)JavaScript addClass不使用PHP GET[';页面';],javascript,php,jquery,Javascript,Php,Jquery,我一直在尝试实现以下javascript,将最后单击的列表项设置为“active”类,并从其他列表项中删除该类(如果适用) <script> $(function() { $('ul li').click( function() { $(this).addClass('active').siblings().removeClass('active'); })
<script>
$(function() {
$('ul li').click( function() {
$(this).addClass('active').siblings().removeClass('active');
});
});
</script>
$(函数(){
$('ul li')。单击(函数(){
$(this.addClass('active').sides().removeClass('active');
});
});
这段代码可以在静态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>
p{
字体大小:32px;
字体大小:粗体;
文本对齐:居中;
}
#横幅
{
宽度:100%;
}
$(函数(){
$('ul li')。单击(函数(){
$(this.addClass('active').sides().removeClass('active');
});
});
切换导航
我可以使用PHP编写(并且已经编写)相同的功能,但是,我想了解为什么这段代码不起作用,有人有什么想法吗?使用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');
});
});
在页面导航时附加click事件将不允许jQuery执行 相反,当php执行时,在列表项上添加一个活动类
不工作,我想php页面包含的值会被重置,但我不确定
<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>