簡體   English   中英

頁面加載時隱藏並顯示div

[英]Hide and show div when page load

我有一個div:

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

當頁面加載時,我嘗試隱藏div:

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

任何人都可以通過lbStatus.Text = Close來隱藏此div

您不能為此簡單地使用CSS嗎?

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

嘗試使用$(document).ready ,它在加載HTML-Document且DOM准備就緒時執行,而window.onload在完全加載完整頁面(包括所有框架,對象和圖像)時執行

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

當您使用Asp.Net嘗試使用ClientId屬性

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

已更改<%=lbStatus.ClientID%>而不是lbStatus

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

您混合使用純JavaScript和jQuery。
如果您不包括jquery庫,請使用純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>

看來您需要刪除#號。

$('postreply')。hide();

或者,香草Javascript:

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

您可以從一開始就將其隱藏。

使用javascript document.getElementById('lbStatus').style.display = 'none';document.getElementById('lbStatus').style.display = 'none'; 並將其恢復為“可見”,請使用document.getElementById('lbStatus').style.display = "";

或使用CSS#lbStatus{display: none;}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM