如何在使用jQuery加载页面之前隐藏HTML元素

nfzehxib  于 2023-03-07  发布在  jQuery
关注(0)|答案(8)|浏览(178)

我有一些显示或隐藏div的JQuery代码。

$("div#extraControls").show();   // OR .hide()

我最初希望div不可见,所以我使用了:

$(document).ready(function() {
    $("div#extraControls").hide();
});

但是,在浏览器上,内容加载可见一秒钟后才消失,这不是我想要的。
如何在页面加载之前设置隐藏元素,同时保持使用脚本动态显示隐藏元素的能力?

goqiplq2

goqiplq21#

在CSS3中,您实际上可以利用:only-child选择器隐藏内容,直到页面加载为止。
Here is a demonstration of how to do this。基本思想是CSS在加载时会向DOM添加一个节点,同时加载该节点及其子节点。此外,一旦第二个子节点被添加到给定的父节点,:only-child选择器规则就被打破了。我们匹配此选择器并设置display: none以隐藏给定的节点。

<!doctype html>

<html>

  <head>
    <title>Hide content until loaded with CSS</title>
    <style type="text/css" media="screen">
      .hide-single-child > :only-child {
        display: none;
      }
    </style>
  </head>

  <body>
    <div class='hide-single-child'>
      <div>
        <h1>Content Hidden</h1>
        <script>
          alert('Note that content is hidden until you click "Ok".');
        </script>
      </div>
      <div></div>
    </div>
  </body>

</html>
jucafojl

jucafojl2#

在CSS中:

#extraControls { display: none; }

或者在HTML中:

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

zvms9eto3#

#toggleMe //Keep this in your .css file
{
display:none;
visibility:inherit;
background-color:#d2d8c9;    
}

 <a href="#" onclick="return false;">Expand Me</a>
 ///this way u can make a link to act like a button too

 <div id="toggleMe">  //this will be hidden during the page load
  //your elements
 </div>

///if you decide to make it display on a click, follow below:
<script type="text/javascript" src="jquery.js"> </script> //make sure u include jquery.js file in ur project
<script type="text/javascript">
 $(document).ready(function () {
        $("a").click(function () {
            $('#toggleMe').toggle("slow"); //Animation effect
        });
    });
</script>

///Now for every alternate click, it shows and hides, but during page load; its hidden.
//Hope this helps you guys ...
j7dteeu8

j7dteeu84#

在页面上加载javascript函数的最佳方法是

$(window).bind("load", function() {
   // code here
});

就你而言

div.hidden
{
   display: none
}

<div id="extraControls" class="hidden">
</div>

$(window).bind("load", function() {
   $("div#extraControls").removeClass("hidden");
});
vlf7wbxs

vlf7wbxs5#

在html代码的最顶端添加以下样式。

<style type="text/css" media="screen">
    div.extraControls{display: none !important;}
</style>

并在大多数HTML代码的末尾添加以下脚本。

<script type="text/javascript">
    $(document).ready( function() {
        $('div.extraControls').show();
    });
</script>

不要忘记在div元素中添加class="extraControls"
您也可以使用id代替class。
对于ID,将div.extraControls替换为div#extraControls,将$('div.extraControls')替换为$('div#extraControls')

如果浏览器上禁用Javascript,请使用以下脚本强制启用Javascript。

在GC上检测

<noscript>
<style>
body, html{overflow:hidden}
     #JsEnable {display:none}
/* Enable Javascript Popup by key2blogging */
         #Key2bloggingNoscript {background:rgba(0,0,0,0.85);padding:0;position:fixed;bottom:0;left:0;top:-100px;right:0;z-index:1000;opacity:1;visibility:visible;height:auto;}
         #Key2bloggingNoscript svg {width:100px; height:100px}
         #Key2bloggingNoscript svg path {fill:#fff}
         #Key2bloggingNoscript .isiNoscript{background-color:#34495E;color:#fff;position:absolute;text-align:center;padding:0 30px 30px 30px;margin:auto;top:30%;left:0;right:0;font-size:1.5rem;font-weight:400;line-height:1.5em;max-width:670px;box-shadow:0 20px 10px -10px rgba(0,0,0,0.15);border:15px solid rgba(0,0,0,.07); overflow:hidden; transition:all .6s cubic-bezier(.25,.8,.25,1); -webkit-transform:translateZ(0); transform:translateZ(0); backface-visibility:visible; transition:all .2s ease-in-out,visibility 0s; transform-origin:bottom center; pointer-events:auto; transform:rotateX(0deg) rotateY(0deg) rotateZ(0deg) scale(1); opacity:1; animation:key2BloggingWobble .5s; -moz-animation: key2BloggingWobble .5s; -webkit-animation:key2BloggingWobble .5s; -o-animation:key2BloggingWobble .5s}
         #Key2bloggingNoscript .isiNoscript:hover{box-shadow:0 20px 10px -10px rgba(0,0,0,0.2);}
         #Key2bloggingNoscript .isiNoscript h4, #Key2bloggingNoscript .isiNoscript .JSEnable{display:inline-block;background:rgba(0,0,0,.07);padding:5px 25px 15px 25px;font-size:2.2rem;font-weight:500;margin-bottom:20px}
    </style>
</noscript>

<noscript>
<div id='Key2bloggingNoscript'>
<div class='isiNoscript'><span class='JSEnable'>Enable Javascript</span>
<br/>
<svg viewBox='0 0 24 24'>
<path d='M3,3H21V21H3V3M7.73,18.04C8.13,18.89 8.92,19.59 10.27,19.59C11.77,19.59 12.8,18.79 12.8,17.04V11.26H11.1V17C11.1,17.86 10.75,18.08 10.2,18.08C9.62,18.08 9.38,17.68 9.11,17.21L7.73,18.04M13.71,17.86C14.21,18.84 15.22,19.59 16.8,19.59C18.4,19.59 19.6,18.76 19.6,17.23C19.6,15.82 18.79,15.19 17.35,14.57L16.93,14.39C16.2,14.08 15.89,13.87 15.89,13.37C15.89,12.96 16.2,12.64 16.7,12.64C17.18,12.64 17.5,12.85 17.79,13.37L19.1,12.5C18.55,11.54 17.77,11.17 16.7,11.17C15.19,11.17 14.22,12.13 14.22,13.4C14.22,14.78 15.03,15.43 16.25,15.95L16.67,16.13C17.45,16.47 17.91,16.68 17.91,17.26C17.91,17.74 17.46,18.09 16.76,18.09C15.93,18.09 15.45,17.66 15.09,17.06L13.71,17.86Z'/>
</svg>
<br/>Javascript is disabled on your Browser. To use <data:blog.title.escaped/> turn on Javascript in the browser settings.</div>
</div>
</noscript>
2g32fytz

2g32fytz6#

你可以做的是在文档的末尾插入一个内嵌的script标签,或者直接在id为“extraControls”的div后面插入。

<div id="extraControls">i want to be invisible!</div>
<script type="text/javascript">$("div#extraControls").hide()</script>

当然,你也可以在服务器端这样做,但也许你有一个很好的理由不想这样做(比如,如果浏览器不支持javascript,让控件可见)。

gt0wga4j

gt0wga4j7#

div.hidden
{
   display: none
}

<div id="extraControls" class="hidden">
</div>

$(document).ready(function() {
    $("div#extraControls").removeClass("hidden");
});
bksxznpy

bksxznpy8#

创建一个css类

.hidden {
display: none;
}

把这个添加到任何你不想在加载页面时显示的元素中,然后使用$("div#extraControls").show();再次显示它。

相关问题