简体   繁体   中英

Inline Javascript is breaking my HTML/CSS

This is my first attempt to use jquery, javascript and HTML/CSS in one site. I have a working inline script tag that utilizes jquery and javascript. Unfortunately I can't seem to create ANY structure on the page (an h1 title, an aside, moving the rendered javascript anywhere...) while this script is in my index.html. I've done lots of research but there is some fundamental I'm missing here as I can have only one or the other. Can anyone point me in the right direction? Here's a curtailed section:

<body>
<script>
  $(document).ready(function(){
    var $body = $('body');
    $body.html('');
    //code code code
    }
  });
</script>

I've tried putting my html within the body and outside of the script, in the script but outside of the jquery function, and inside the jquery function. How could I get a simple "Hello World" up on this page?

What's happening is that once the page is finished loading, or ready , you are setting the HTML to nothing via $body.html('') .

You could try changing it to $body.append('This was generated by jQuery!') and you'll see your hardcoded HTML in addition to some text.

Your Script is a Child of the Body, which inner html you are clearing by asigning an empty String - deleting your inline Script in the process. Have you tried moving your Script to the Head section?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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