html - JavaScript- cannot change style when my script in head -


i'm trying javascript work when insert script head or body elements.

here examples:


firstly insert body this example (working):

    <html>      <body>            <p id="p2">hello world!</p>            <script>      document.getelementbyid("p2").style.color = "blue";      </script>            <p>the paragraph above changed script.</p>            </body>      </html>


when move script end of body (also working):

<html>  <body>    <p id="p2">hello world!</p>    <p>the paragraph above changed script.</p>    <script>  document.getelementbyid("p2").style.color = "blue";  </script>  </body>  </html>


but when move head stops working:

	<html>  	<head>  		<script>  		document.getelementbyid("p2").style.color = "blue";  		</script>  	</head>  	<body>    	<p id="p2">hello world!</p>    	<p>the paragraph above changed script.</p>    	</body>  	</html>

you can use window.onload means invoke after html page loaded. here quick reference study . thank you!


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -