Can I target an item before it is closed?

I want to add a class to the body tag without waiting for the DOM to load, but I want to know if the following approach will be valid. I'm more concerned about reality than browsers now support.

<body>
  $("body").addClass("active");
  ...
</body>

Thanks Steve

+3
source share
6 answers

. The elementReady () plugin seems to be very close to what you are looking for.

It works with a loop setIntervalthat completes as soon as it document.getElementById()returns an element for the given id.

Perhaps you can make a small modification to this plugin (or commit an update / patch) to allow common selectors (at least for "tagNames") instead of just ids.

, - - setInterval hacking

javascript , , @JasonBunting, .

+2

DOM, , . $(document).ready(), . , .

<body>
   <div id='topStories'></div>
   <script type='text/javascript'>
     $('div#topStories').addClass('active');
   </script>
</body>

, $(document).ready().

+2

: . -, , , , . :

<html>
<head>
    <style type="text/css">
        .foobar { background-color: #CCC; }
    </style>
</head>
<body>
    <script type="text/javascript">
        window.document.body.className = "foobar";
    </script>
    <div style="border: solid 1px"><br /></div>
    <script type="text/javascript">
    // happens before DOM is fully loaded:
        alert(window.document.body.className);
    </script>
    <span>Appears after the alert() call.</span>
</body>
</html>

IE 7, alert(), , ( , DOM ).

Firefox alert().

, , .

+2

, - . IE6 Firefox 2 ( ), DOM , ( XHTML). , jQuery , , , , "" . :

<script>
  $(document).ready(function() {
    $("body").addClass("active");
  });
</script>
<body>
  ..
  ..
  ..
</body>

javascript.

, :

<body class="active">
</body>
+1

.

.

, JavaScript , JavaScript. , , DOM , , . , "" body , , JavaScript, , , .

, , , , Firefox4, .

, , , , ficker (, ). JS, - ...

0
source

Instead of adding a class to your <body>, it might be easier for you to add a class to your <html> tag:

<script type="text/javascript">
    document.documentElement.className = 'active';
</script>
0
source

Source: https://habr.com/ru/post/1698772/


All Articles