简体   繁体   中英

Hide and show div when page load

i have a div :

<div id="postreply">
    <asp:Label ID="lbStatus" CssClass="input-large1" runat="server" Text="Close" Width="600px"></asp:Label>
</div>

i try to hide div when page load :

<script type="text/javascript">
window.onload = function() {            
        var x = document.getElementById('lbStatus').innerText;
        if(x == "Close"){
            $("#postreply").hide();
        }
    }</script>

anyone help me hide this div with lbStatus.Text = Close

Can't you simply use CSS for this?

#postreply {
   display: none; /* onLoad your div will be hidden */
}

Try this, once with $(document).ready , it executes when HTML-Document is loaded and DOM is ready where as window.onload executes when complete page is fully loaded, including all frames, objects and images

$(document).ready(function() {
    if($("#lbStatus").val() == "Close"){
        $("#postreply").hide();
    }
});

As you are using Asp.Net try to use ClientId property

$(document).ready(function() {
    if($("#<%=lbStatus.ClientID%>").val() == "Close"){
        $("#postreply").hide();
    }
});

Changed <%=lbStatus.ClientID%> instead of lbStatus

Reference: http://4loc.wordpress.com/2009/04/28/documentready-vs-windowload/

You mix up between pure javascript and jQuery.
If you not include jquery library, use pure javascript.

<script type="text/javascript">
  window.onload = function() {            
    var x = document.getElementById('lbStatus').innerText;
    if(x == "Close"){
      // $("#postreply").hide();
      document.getElementById('postreply').style.display = 'none';
    }
  }
</script>

Looks like you need to remove the # sign.

$('postreply').hide();

Or, vanilla Javascript:

document.getElementById('postreply').style.display = 'none';

You can hide it from the start.

Either with javascript : document.getElementById('lbStatus').style.display = 'none'; and to get it back "visible" use document.getElementById('lbStatus').style.display = "";

or with css : #lbStatus{display: none;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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