0

我想在我的网站页面上有一个标准菜单。我正在使用包含文件,如下所示:

<div class="header box">
<ul class="nav box">
<li class="current" id="home"><a href="index.php" onclick="current('home')">Home</a></li>
<li class="" id="onlineSchedule"><a href="onlineSchedule.php" onclick="current(this)">Online Schedule</a></li>
<li class="" id="partners"><a href="partners.php" onclick="current(this)">Partners/Centers</a></li>
<li class="" id="services"><a href="services.php" onclick="current(this)">Services</a></li>
<li class="" id="products"><a href="products.php" onclick="current(this)">Products</a></li>
<li class="" id="newsHighlights"><a href="newsHighlights.php" onclick="current(this)">News/Highlights</a></li>
<li class="" id="signup"><a href="signup.php" onclick="current(this)">Sign Up!</a></li>
<li class="" id="feedback"><a href="feedback.php" onclick="current(this)">Feedback</a></li>
</ul>

但是我希望每当我单击不同的页面时该类都会更改,因为“当前”类会突出显示菜单中的链接。我该怎么做呢?我在下面尝试了 Javascript 作为 shwon 但它不起作用......提前谢谢

function current(page)
{
    //alert(page);
    document.getElementById("home").setAttribute("class", "");
    document.getElementById("onlineSchedule").setAttribute("class", "");
    document.getElementById("partners").setAttribute("class", "");
    document.getElementById("services").setAttribute("class", "");
    document.getElementById("products").setAttribute("class", "");
    document.getElementById("newsHighlights").setAttribute("class", "");
    document.getElementById("signup").setAttribute("class", "");
    document.getElementById("feedback").setAttribute("class", "");

    document.getElementById(page).className += "current";
    var temp = document.getElementById("home").getAttribute("class");
    alert(temp);
}
4

1 回答 1

1

这是工作代码的示例:

<style type="text/css">
.current {
    color: red;
}
.current a {
    color: green;
}
</style>
<div class="header box">
<ul class="nav box">
<li class="current" id="home"><a href="index.php" onclick="current('home')">Home</a></li>
<li class="" id="onlineSchedule"><a href="onlineSchedule.php" onclick="current(this)">Online Schedule</a></li>
<li class="" id="partners"><a href="partners.php" onclick="current(this)">Partners/Centers</a></li>
<li class="" id="services"><a href="services.php" onclick="current(this)">Services</a></li>
<li class="" id="products"><a href="products.php" onclick="current(this)">Products</a></li>
<li class="" id="newsHighlights"><a href="newsHighlights.php" onclick="current(this)">News/Highlights</a></li>
<li class="" id="signup"><a href="signup.php" onclick="current(this)">Sign Up!</a></li>
<li class="" id="feedback"><a href="feedback.php" onclick="current(this)">Feedback</a></li>
</ul>
<script type="text/javascript">
function current(page)
{
    document.getElementById("home").setAttribute("class", "");
    document.getElementById("onlineSchedule").setAttribute("class", "");
    document.getElementById("partners").setAttribute("class", "");
    document.getElementById("services").setAttribute("class", "");
    document.getElementById("products").setAttribute("class", "");
    document.getElementById("newsHighlights").setAttribute("class", "");
    document.getElementById("signup").setAttribute("class", "");
    document.getElementById("feedback").setAttribute("class", "");

    document.getElementById(page).className = "current";
}

current("home");
</script>

我假设你也想格式化链接,所以我展示了如何通过添加.current a { }你将为当前链接放置 CSS 的位置。.current { }主要只会影响列表项。

于 2012-10-09T03:21:54.100 回答