gpt4 book ai didi

c# - 在 Site.master 回发事件中保存
  • 类值
  • 转载 作者:行者123 更新时间:2023-11-29 22:06:05 24 4
    gpt4 key购买 nike

    我想更改 <LI> 的类单击时,我设法用 jquery 做到了(我是 jquery 的新手)

    代码:

      <script type='text/javascript'>
    $(function () {
    $('#Mymenu li').click(function () {
    $('li.active').removeClass('active');
    $(this).addClass('active');
    });
    });
    </script>

    它的工作非常完美。上面的 Jquery 代码删除了 li 的类并将其分配给其他类,但是一旦发生重定向/回发,它就会丢失该值并返回到默认值。

    任何想法谢谢

    最佳答案

    您可以将当前选定的 li 的值存储在 cookie、localStorage 或 window hash 中,并在页面重新加载时检索该值。

    查看窗口哈希示例

    <script type="text/javascript">
    $(function () {
    $('li.active').click(function () {
    //tweak this line

    window.location.hash = $(this).attr('id');
    $("li.active").removeClass("active")
    $(this).addClass('active');
    });
    var hash = window.location.hash;
    $("#"+hash).removeClass("active")
    $("#"+hash).addClass('active');
    });
    </script>

    更新查看完整示例

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.9.1.js"></script>
    <script type="text/javascript">

    $(function () {
    $('li').click(function () {
    //tweak this line

    window.location.hash = $(this).index();
    $("li.active").removeClass("active")
    $(this).addClass('active');
    });
    var suffix = window.location.hash.match(/\d+/);
    $("li:eq(" + suffix + ")").removeClass("active")
    $("li:eq(" + suffix + ")").addClass('active');
    });
    </script>

    <style>
    .active {
    color: red;
    }
    </style>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <ul>
    <li>A
    </li>
    <li>B
    </li>
    <li>C </li>
    <li>D
    </li>
    </ul>
    </div>
    </form>
    </body>
    </html>

    关于c# - 在 Site.master 回发事件中保存 <LI> 类值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20935386/

    24 4 0