页面加载时隐藏并显示div

时间:2022-12-01 15:21:20

i have a div :

我有一个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 :

我在页面加载时尝试隐藏div:

<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

任何人帮我隐藏这个div与lbStatus.Text =关闭

5 个解决方案

#1


3  

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,它会在加载HTML文档并且DOM准备就绪时执行,当完整页面完全加载时执行window.onload,包括所有框架,对象和图像

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

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

当您使用Asp.Net时尝试使用ClientId属性

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

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

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

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

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

#2


5  

Can't you simply use CSS for this?

难道你不能简单地使用CSS吗?

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

#3


2  

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

你在纯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>

#4


1  

Looks like you need to remove the # sign.

看起来你需要删除#符号。

$('postreply').hide();

$( 'postreply')隐藏()。

Or, vanilla Javascript:

或者,vanilla Javascript:

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

#5


0  

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 = "";

使用javascript:document.getElementById('lbStatus')。style.display ='none';并使其返回“可见”使用document.getElementById('lbStatus')。style.display =“”;

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

或者使用css:#lbStatus {display:none;}

#1


3  

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,它会在加载HTML文档并且DOM准备就绪时执行,当完整页面完全加载时执行window.onload,包括所有框架,对象和图像

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

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

当您使用Asp.Net时尝试使用ClientId属性

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

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

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

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

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

#2


5  

Can't you simply use CSS for this?

难道你不能简单地使用CSS吗?

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

#3


2  

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

你在纯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>

#4


1  

Looks like you need to remove the # sign.

看起来你需要删除#符号。

$('postreply').hide();

$( 'postreply')隐藏()。

Or, vanilla Javascript:

或者,vanilla Javascript:

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

#5


0  

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 = "";

使用javascript:document.getElementById('lbStatus')。style.display ='none';并使其返回“可见”使用document.getElementById('lbStatus')。style.display =“”;

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

或者使用css:#lbStatus {display:none;}