gpt4 book ai didi

javascript - 更改母版页中菜单项的类别

转载 作者:太空宇宙 更新时间:2023-11-04 14:13:11 24 4
gpt4 key购买 nike

我在母版页中有一个菜单项 .. 我使用 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/

24 4 0
文章推荐: javascript - 我无法让容器
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com