一个TextBox,一个Calendar控件,我想当TextBox得到焦点时Calendar控件就隐藏起来,我下面的代码老是隐藏不了?C#:this.tbInHospitalDateBegin.Attributes.Add("onfocus", "calVisible(" + this.Calendar2.ClientID+ ")");js:
    function calVisible(obj1) {
         obj1.style.display="none";
        //alert("1020");
    }
不知道哪里写得不对??

解决方案 »

  1.   

    要写在pageload中。能获取到对象不?
      

  2.   

    TextBox1.Attributes.Add("onfocus", "hideCalendar('" + Calendar1.ClientID + "');");<script type="text/javascript">
            function hideCalendar(id) {
                document.getElementById(id).style.display = "none";
            }
    </script>
      

  3.   


    试过了,不行,不知道哪里没写好,用alert("1020")放在hideCalendar()都有效果,就是  document.getElementById(id).style.display = "none"没有,不知跟ajax的updatepannel面板有没有关系,因为我放有updatepannel面板
      

  4.   

    这代码是我刚试过的.不过我没有UpdatePanel.你的Calendar是在UpdatePanel里吗?
      

  5.   

    我刚又加了个ScriptManager, 再把TextBox和Calendar拖进UpdatePanel里.没问题呀!
      

  6.   

                       <div id="divcal1" class="calender1" runat="server">
                                    <asp:Calendar ID="Calendar1" runat="server" 
                                          ...
                                    </asp:Calendar>
                        </div>div外层有UpdatePanel
      

  7.   

    我试的代码是这样:<head runat="server">
        <title></title>
        <script type="text/javascript">
            function hideCalendar(id) {
                document.getElementById(id).style.display = "none";
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
                </ContentTemplate>
            </asp:UpdatePanel>
            
        </div>
        </form>
    </body>C#protected void Page_Load(object sender, EventArgs e)
    {
            TextBox1.Attributes.Add("onfocus", "hideCalendar('" + Calendar1.ClientID + "');");
    }
      

  8.   

    div是用绝对定位有关系没?
         .calender1
        {
         position:absolute;
         left:44%;
         top:20%;
        }
      

  9.   

    如果是asp.net ajax的话<script type="text/javascript">
            function hideCalendar(id) {
                $get(id).style.display = "none";
            }
    </script>
      

  10.   

    其实,楼主只要看看能不能获取到这个对象。而且你又放在一个div中,隐藏div就行了。
      

  11.   

    我把你的css也加进去了. 没问题呀.<head runat="server">
        <title></title>
        <script type="text/javascript">
            function hideCalendar(id) {
                $get(id).style.display = "none";
            }
        </script>
        <style type="text/css">
          .calender1 
        { 
        position:absolute; 
        left:44%; 
        top:20%; 
        }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <div id="divcal1" class="calender1" runat="server"> 
                    <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
                    </div>
                </ContentTemplate>
            </asp:UpdatePanel>
            
        </div>
        </form>
    </body>
    C#和刚才一样.
      

  12.   

    不过这个和用$get()无关的,这个你得明白. $get仅仅是asp.net ajax框架上document.getElementById的缩写形式而已. 它俩等价的.
      

  13.   

    那为什么我用document.getElementById不行呢