2009-11-30 58 views
2

這是我的代碼:JQuery:如何在文本框不在焦點時隱藏DIV?

<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:TextBox ID="txtInsertComments" CssClass="expanding" runat="server" AutoPostBack="false" TextMode="MultiLine"></asp:TextBox> 
    </div> 
     <div id="commands"> 
      <table cellpadding="0" cellspacing="0" width="400px" id="tblCommands"> 
       <tr> 
        <td style="width: 50%; text-align: center;"> 
         <asp:LinkButton ID="lnbInsertRes" runat="server" CommandName="Insert" Font-Underline="false">Insert</asp:LinkButton> 
        </td> 
        <td style="width: 50%; text-align: center;"> 
         <asp:LinkButton ID="lnbCancelRes" runat="server" CommandName="Cancel" Font-Underline="false">Cancel</asp:LinkButton> 
        </td> 
       </tr> 
      </table> 
     </div> 
    </form> 
</body> 
    <script type="text/javascript"> 
     $(function() 
     { 
      $("#commands").hide("normal"); 

      $("textarea[id$=txtInsertComments]").focus(function() 
      { 
       $("#commands").show("normal"); 
      }); 

     }); 
    </script> 
</html> 

首先,這個頁面載入時,在div #command加載非常快,然後消失。我需要該div保持隱藏狀態,直到文本框txtInsertComments控件被點擊或獲得焦點。

二,當用戶點擊文本框txtInsertComments或文本框失去焦點時,div#命令仍然存在,並且不隱藏。

任何幫助表示讚賞。

回答

4

您可以使用CSS來隱藏#command DIV,直至要顯示它:

<div id="commands" style="display:none"> 

然後使用模糊/焦點顯示/隱藏#命令DIV:

$('#txtInsertComments').blur(function() { 
    $("#commands").hide() 
}).focus(function() { 
    $("#commands").show() 
}); 
1

設置display:none最初在#命令上。

<div id="commands" style="display:none">.... 

注意力不集中,只需使用模糊事件:

$(el).blur(function(){...}); 
1
$(document).ready(function() { 
    $('#<%=txtInsertComments.ClientID%>').blur(function() 
     { $("#commands").hide() }); 
    $('#<%=txtInsertComments.ClientID%>').focus(function() 
     { $("#commands").show() }); 
}); 
+0

非常感謝您的快速回復。有用! – Chuck 2009-11-30 17:06:44

+0

好的,你們給我看的代碼適用於基本的asp.net頁面。有沒有人有使用jQuery嵌套ListView控件的經驗?這個相同的jQuery不適用於嵌套的ListView控件。 – Chuck 2009-11-30 17:23:44

+0

非常感謝;)此代碼是完美的;) – Schewns 2012-12-26 16:17:30