简体   繁体   中英

Cant get .innerHTML to work

I am working with ruby on rails and I want to test some things out.

I have used this simple code to change the innerHTML of my body:

document.body.innerHTML = "Work will you?";

It doesn't bring any results.

I know my javascript file works correctly because i tested it with the: alert() function

When i put the document.body.innerHTML into my file it breaks the javascript (the alert no longer works)

Anybody knows whats could cause this??

You must be running your code from within the <head> tag. document.body doesn't exist at that point. You have to wait for the DOM to be ready. You can do that using something like jQuery's $(document).ready() http://jsfiddle.net/L83mL/

Credit to https://stackoverflow.com/users/1013842/david and https://stackoverflow.com/users/400654/kevin-b

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