2012-06-23 98 views
2

我正在使用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"); 
      }); 
     }); 

請指導我。

+0

你在這裏有什麼問題?以及重點在哪裏定義? – Aristos

+0

是母版頁上的標籤? –

+0

是的,我的標籤只在主頁上,我只需要使用客戶端代碼。 –

回答

2

的ID如果你有一個標籤服務器控件,在回發之前用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> 
+0

其工作很好謝謝你..! –

1

如果我正確地解決了問題,標籤位於母版頁上,並且$('#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客戶端頁面的值賦給標籤 - 用我提供的

0

如果你想用戶看到點擊你需要阻止瀏覽器的默認,並設置計時器的鏈接後,東西你想要的時間。

這不會提供非常好的用戶體驗!

$('#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; 
    } 

}); 
相關問題