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

标签 javascript jquery asp.net css menu

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

相关文章:

asp.net - 如何拦截 ASP.net webform 中的身份验证请求

javascript - 关于在 jQuery 中工作的更改事件

javascript - IBM Worklight 6.1 将客户端日志写入 messages.log

javascript - 如何选择单击的按钮来更改它的类

javascript - JQuery 的容器结构问题 - 包含容器

javascript - 如何在 d3 树布局中仅显示特定节点级别

asp.net - 创建希腊数据

javascript - Google Speech API - 长音频文件的服务器不可用错误

javascript - Laravel Ajax Input::all() 通过 FormData 发送时返回空

asp.net - 对象数据源