简体   繁体   English

文件准备好之前是否隐藏元素?

[英]Hiding element before document is ready?

I have a div that needs to be hidden by default. 我有一个div,默认情况下需要隐藏。 It then can be toggled by a button: 然后可以通过按钮进行切换:

    <script type="text/javascript">
        function toggle() {
            text = document.getElementById('add_view');
            var isHidden = text.style.display == 'none';
            text.style.display = isHidden ? 'block' : 'none';
        }


        $(document).ready
    (

              function () {
                  toggle();
                  $("#add_view, #btnToggle").click(function (e) {
                      e.stopPropagation();
                  });
                  $(document).click(function () {
                      toggle();
                  });
              }
    );


</script>

It is working fine. 一切正常。 The only problem is, when I refresh the page, I momentarily see the div before it is hidden. 唯一的问题是,当我刷新页面时,在隐藏它之前,我会暂时看到div。

What could I do to prevent that? 我该怎么做才能防止这种情况?

Thanks 谢谢

You probably need to hide your element by default, and then use the button to toggle visibility. 默认情况下,您可能需要隐藏元素,然后使用按钮切换可见性。 Try this: 尝试这个:

<div id="add_view" style="display:none">....</div>

首先将元素隐藏在html中。

<div id="add_view" style="display: none;"></div>

Initially, you have to hide it by setting style="display:none;" 最初,您必须通过设置style="display:none;"来隐藏它style="display:none;" of the div. div。 Once when u want to toggle it, you have to use it as 一旦您想切换它,就必须将其用作

document.getElementById(Id).style.display=""; 

in javascript. 在javascript中。

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

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