【问题标题】:How to perform click event on anchor tags如何在锚标签上执行点击事件
【发布时间】:2012-06-23 08:59:13
【问题描述】:

我正在使用 c# 开发 asp.net。我有一个带有 3 个锚标记链接和一个 html 标签的母版页。当用户单击特定链接时,它应重定向到特定页面,并且链接文本应显示在标签上。我使用了以下代码,

$(document).ready(function () {
            $('#div1 a').click(function () {
                if (focus == true)
                    $('#lblHeader').text("Home");
            });
            $('#blog').click(function () {
                if (focus == true)
                    $('#lblHeader').text("Blog");
            });
            $('#about').click(function () {
                if (focus == true)
                    $('#lblHeader').text("About");
            });
        });

请指导我。

【问题讨论】:

  • 您的问题是什么?以及重点在哪里定义?
  • 是母版页上的标签吗?
  • 是的,我的标签只在母版页上,我只需要使用客户端代码。
  • 我遇到了同样的事情,我的回答应该会有所帮助
  • 真的不清楚链接的行为需要是什么,如果想要下一页显示价值需要使用cookie,localStorage或发送数据到服务器

标签: jquery asp.net html


【解决方案1】:

如果你有一个标签服务器控件,在回发发生之前用 javascript 更改文本将没有任何效果,因为整个页面都被重绘了。

编辑:我想我知道你想做什么。为什么不在母版页中创建contentplaceholder,并在每个页面中设置其值?

在您的母版页中:

<%@ Master Language="C#" %>
...
<asp:contentplaceholder id="PageLabel" runat="server" />

在每个内容页面中:

<asp:Content ID="Content1" ContentPlaceHolderID="PageLabel" Runat="Server" >
    Home <!--or about, or blog-->
</asp:content>

【讨论】:

    【解决方案2】:

    如果我解决了您的问题,则标签位于母版页上并且 $('#lblHeader') 未正确访问标签,可能是因为 asp.net 更改了母版页标签的 ID - 尝试这个:

           $(document).ready(function () {
            $('#div1 a').click(function () {
                if (focus == true)
                     $('[id$=lblHeader]').text("Home");
            });
            $('#blog').click(function () {
                if (focus == true)
                     $('[id$=lblHeader]').text("Blog");
            });
            $('#about').click(function () {
                if (focus == true)
                     $('[id$=lblHeader]').text("About");
            });
        });
    

    或将该值存储在隐藏字段中,并在客户端页面的 document.ready 中将该值分配给标签 - 使用我提供的 id

    【讨论】:

      【解决方案3】:

      如果您希望用户在单击链接后看到某些内容,您需要阻止浏览器默认设置并设置所需的持续时间。

      这不会提供很好的用户体验!

      $('#div1 a, #blog, #about').click(function() {
          if (focus == true) {
              $('[id$=lblHeader]').text("Home");
              var href = this.href
              setTimeout(function() {
                  window.location = href;
              }, 2000);
              return false;
          }
      
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-02
        • 1970-01-01
        • 1970-01-01
        • 2013-12-02
        • 2011-11-17
        • 2019-02-15
        • 1970-01-01
        相关资源
        最近更新 更多