在PHP中将导航作为一个单独的页面



我正在使用我的导航作为一个单独的页面,即Navigation.php,并在每个页面中通过<?php include 'header.php'; ?>添加此页面
但当我进入一个页面时,例如:进入About页面,导航栏中的About我要进入二级页面时,问题就来了,比如:
Portfolio>Portfolio Item
该脚本没有在Portfolio按钮上添加类

Navigation.php页面:

<ul class="nav nav-pills" id="mainNav">
<li <?php echo (basename($_SERVER['PHP_SELF']) == 'index.php') ? 'class="active"' : ''; ?>>
<a class="nav-item nav" href="/home">
Home
</a>
</li>
<li <?php echo (basename($_SERVER['PHP_SELF']) == 'about.php') ? 'class="active"' : ''; ?>>
<a class="nav-item"  href="/about">
About
</a>
</li>
<li <?php echo (basename($_SERVER['PHP_SELF']) == 'services.php') ? 'class="active"' : ''; ?>>
<a class="nav-item"  href="/services">
Services
</a>
</li>
<li <?php if ((basename($_SERVER['SCRIPT_FILENAME'], '.php')) == 'portfolio' or 'portfolioItem') echo "class='active'";?>>
<a class="nav-item"  href="/portfolio">
Portfolio
</a>
</li>
<li <?php echo (basename($_SERVER['PHP_SELF']) == 'showcase.php') ? 'class="active"' : ''; ?>>
<a class="nav-item"  href="/showcase">
Showcase
</a>
</li>
<li <?php echo (basename($_SERVER['PHP_SELF']) == 'blog.php') ? 'class="active"' : ''; ?>>
<a class="nav-item"  href="/blog">
Blog
</a>
</li>
<li <?php echo (basename($_SERVER['PHP_SELF']) == 'contact.php') ? 'class="active"' : ''; ?>>
<a class="nav-item"  href="/contact">
Contact
</a>
</li>
</ul>

Javascript我正在使用:

<script>
$(function () {
var pgurl = window.location.href.substr(window.location.href.lastIndexOf("/") + 1);
$("nav-pills ul li").each(function () {
if ($(this).attr("href") == pgurl) {
$(this).parent().addClass('active');
}
if ((($(this).attr("href") == 'index.php')||($(this).attr("href") == '')) && (pgurl == '')) {
$(this).parent().addClass("active");
}
})
});
</script>

请尝试更改

<?php if ((basename($_SERVER['SCRIPT_FILENAME'], '.php')) == 'portfolio' or 'portfolioItem') echo "class='active'";?>>

<?php if ( (basename($_SERVER['SCRIPT_FILENAME'], '.php')) == 'portfolio.php' || (basename($_SERVER['SCRIPT_FILENAME'], '.php')) == 'portfolioItem.php'  ) { echo "class='active'"; }  ?>>

相关内容

  • 没有找到相关文章

最新更新