我在母版页中有一个菜单项 .. 我使用 javascript 在单击时更改了类 .. 它改变了类一秒钟但是当它重定向到其他页面时它变得相同 .. 知道我如何应该这样做
我现在无法访问完整的代码仪式,但它是这样的(我使用了一些警告框只是为了看看发生了什么......)
<style>
test1{ color: red;
height:1000px;
font-size:5px ;
background-color:Aqua;
}
test2
{ font-family:Kozuka Mincho Pro M;
color: red;
font-size: 5px }
<script type="text/javascript">
var lastid = "";
function myFunc(id) {
alert(id);
if (lastid != "") {
document.getElementById(lastid).removeAttribute("class", "test2");
document.getElementById(lastid).setAttribute("class", "test1");
var a = document.getElementById(lastid);
alert(a.getAttribute("class"));
}
var element = document.getElementById(id);
document.getElementById(id).setAttribute("class", "test2");
var cssid = id;
$("#" + id).addClass('test1');
alert(document.getElementById(id).getAttribute("class"));
lastid = id;
}
</script>
<li id="firstry" onclick="myFunc(this.id);" ><a href="master-child.aspx"> Click</a> </li>
最佳答案
我已经使用 Jquery 完成了这项工作。它让事情变得更容易了。它遍历菜单中的每个链接,然后在与当前页面匹配的地方添加类。 以下是脚本:
<script type="text/javascript">
$(document).ready(function () {
$('#subnav a').each(function (index) {
if (this.href.trim() == window.location)
$(this).addClass("selected");
});
});
</script>
关于javascript - 更改母版页中菜单项的类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20576397/