javascript change innerhtml

前端 未结 7 1904
逝去的感伤
逝去的感伤 2021-02-13 04:04

ok im new at javascript, but im trying to change the innerhtml of a div tag, heres my script and its not working:




        
相关标签:
7条回答
  • 2021-02-13 04:28

    correct:

    window.onload = var1;
    

    in your example value of window.onload is undefined because function var1 returns nothing (undefined). You should set onload property to function var1, not result of calling function var1()

    0 讨论(0)
  • Your example will work if you change the uppercase "L" to a lowercase "L" in "onLoad" and remove the parenthesis after var1 where you currently have window.onLoad = var1();

    0 讨论(0)
  • 2021-02-13 04:39

    Below is another simpler version

    <body>
     <div id="test">change</div>
      <script type="text/javascript">
       document.getElementById('test').innerHTML = 'hi';
     </script>
    </body>

    0 讨论(0)
  • 2021-02-13 04:42

    using .innerHTML is non-standard, and a terrible practice for many reasons. you should create a new element using the proper standard methods and and add it to the tree where you want it

    0 讨论(0)
  • 2021-02-13 04:43

    Rather than assigning var1 to window.onload, you're currently calling the function and storing its result. Also, this might be obvious, but var1 seems like an odd name for a function. Try this:

    function var1() {
      document.getElementById('text').innerHTML = 'hi';
    }
    
    window.onload = var1;
    

    Note the casing of onload, as well as the missing parentheses after var1.

    0 讨论(0)
  • 2021-02-13 04:53

    Try changing onLoad to onload.

    function var1() {
      document.getElementById('test').innerHTML = 'hi';
    }
    window.onload = var1; // onload
    
    0 讨论(0)
提交回复
热议问题