简体   繁体   English

使用jQuery隐藏元素占用页面空间

[英]Hiding elements using jquery taking up space on page

I am doing show and hide of server controls ie Textboxe and DropDownList using jquery. 我在做显示和隐藏服务器控件,即使用jquery的Textboxe和DropDownList。

Show and hide is working fine but the element which is hidden is taking up its blank space on the page . 显示和隐藏工作正常,但是隐藏的元素占用了页面上的空白。

I've tried following tricks to hide the elements after using jquery hide() function: 我尝试过以下技巧,以在使用jquery hide()函数后隐藏元素:

css('visibility', 'hidden') css('可见性','隐藏')

css('display', 'none') css('display','none')

as defined in this Question 本课题所定义

But still the same problem . 但是还是同样的问题。

Here is my code: 这是我的代码:

 <script>
    $(document).on("click", ".edit", function () {
    var col_name= $(this).data('col_name');
    var tbl_name = $(this).data('tbl_name');
    var tr = $(this).parent().parent();
    var tdRecords = $(tr).children();
    var CurrValue = $(tdRecords[0]).text();
    $('#<%= txt_Curr_Val.ClientID %>').val(CurrValue);
    $('#<%=txt_colname.ClientID%>').val(col_name);
    $('#<%=txt_tblname.ClientID%>').val(tbl_name);
        if (col_name == 'relig_code')
        {
            $('#<%=ddl_relig.ClientID%>').show('slow');
            //$('#<%=txt_New_Val.ClientID%>').hide('slow');
            $('#<%=txt_New_Val.ClientID%>').css('visibility', 'hidden')
        }
        else
        {
            //$('#<%=ddl_relig.ClientID%>').hide('slow');
            $('#<%=ddl_relig.ClientID%>').css('visibility', 'hidden')
             $('#<%=txt_New_Val.ClientID%>').show('slow')
        }
    });
  </script>

Here is the HTML : 这是HTML:

<div class="modal-body">
                      <div class="row">
                          <div class="col-md-3">
                              Current Value :
                          </div>
                          <div class="col-md-8">
                              <asp:TextBox CssClass="txtstyle txtwidth"  runat="server" ID="txt_Curr_Val" TextMode="MultiLine"></asp:TextBox>
                          </div>
                      </div>
                        <div class="row">
                          <div class="col-md-3">
                              New Value :
                          </div>
                          <div class="col-md-8">
                              <asp:TextBox CssClass="txtstyle txtwidth" runat="server" ID="txt_New_Val" TextMode="MultiLine"></asp:TextBox><br />
                              <asp:DropDownList  runat="server" ID="ddl_relig"></asp:DropDownList><br />
                              <asp:TextBox CssClass="txtstyle" runat="server" ID="txt_tblname" ></asp:TextBox><br />
                              <asp:TextBox CssClass="txtstyle" runat="server" ID="txt_colname"></asp:TextBox>
                          </div>
                      </div>

                    </div>

After trying display : none 尝试显示后:无

its look like : 它看起来像:

在此处输入图片说明

How can I resolve this ? 我该如何解决?

Thanks for your Help 谢谢你的帮助

You could try: 您可以尝试:

$('#<%=ddl_relig.ClientID%>').css('position', 'absolute')
$('#<%=ddl_relig.ClientID%>').css('left', '-9999px')

The problem seems to be the presence of line breaks ( <br/> ) between the controls. 问题似乎是控件之间存在换行符( <br/> )。 You can replace them by a class style where you set the display mode to block : 您可以通过将显示模式设置为block的类样式替换它们:

.newLine
{
    display: block;
}

The newLine class is applied to each element: newLine类应用于每个元素:

<div class="col-md-8">
    <asp:TextBox CssClass="txtstyle txtwidth newLine" runat="server" ID="txt_New_Val" TextMode="MultiLine" />
    <asp:DropDownList CssClass="newLine" runat="server" ID="ddl_relig" />
    <asp:TextBox CssClass="txtstyle newLine" runat="server" ID="txt_tblname" />
    <asp:TextBox CssClass="txtstyle newLine" runat="server" ID="txt_colname" />
</div>

The show and hide jQuery functions can then be used and will not leave an extra space between the controls: 然后可以使用showhide jQuery函数,并且不会在控件之间留下多余的空间:

$(document).on("click", ".edit", function () {
    ...
    if (col_name == 'relig_code') {
        $('#<%=ddl_relig.ClientID%>').show('slow');
        $('#<%=txt_New_Val.ClientID%>').hide('slow');
    }
    else {
        $('#<%=ddl_relig.ClientID%>').hide('slow');
        $('#<%=txt_New_Val.ClientID%>').show('slow')
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM