jQuery 页面加载时隐藏和显示div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17483726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:11:18  来源:igfitidea点击:

Hide and show div when page load

javascriptjquery

提问by beginerdeveloper

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

谁能帮我用 lbStatus.Text = Close 隐藏这个 div

回答by Mr. Alien

Can't you simply use CSS for this?

你不能简单地使用 CSS 吗?

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

回答by Satpal

Try this, once with $(document).ready, it executes when HTML-Document is loaded and DOM is ready where as window.onloadexecutes 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.Nettry to use ClientIdproperty

当您使用时,请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/

回答by diewland

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>

回答by Ben

Looks like you need to remove the #sign.

看来您需要删除该#标志。

$('postreply').hide();

$('postreply').hide();

Or, vanilla Javascript:

或者,香草 Javascript:

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

回答by Sergio

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;}