Uncaught TypeError: Cannot read property 'getElementsByTagName' of null

后端 未结 2 1001
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 03:20

I\'m trying to select elements with .getElementsByTagName().

var tags = document.body.getElementsByTagName(\"*\");

Current attemp

2条回答
  •  故里飘歌
    2021-02-04 03:49

    Function examples:

    • Page loaded with pure JavaScript

    • Page loaded with jQuery

    Example with both methods:

    function init() {
      alert("hello javascript");
    };
    
    window.addEventListener("load",init,false);
    
    $(document).ready(function () {
      alert("hello jQuery");
    });
    

    http://jsfiddle.net/Ln5zu4Lt/

    Be careful, the JavaScript example above waits for page load (images, iframes, etc.). This takes more time than jQuery's ready.

提交回复
热议问题