Cannot set up position js dom programming Art in javascript Chapter 10 activity

topic description

this is an exercise in the art of js dom programming. I want some animation, but I can"t set the style property of the element with js;

related codes

/ / Please paste the code text below (do not replace the code with pictures)

< html >

<head>
    <meta charset="utf-8">
    <title></title>
    <script type="text/javascript">
        function addLoadElement(func){
            var oldonload = window.onload;
            if(typeof window.onload != "function"){
                window.onload = func;
            }else{
                window.onload = function(){
                    oldonload();
                    func();
                }
            }
        }
        function positionMessage(){
            if(!document.getElementsByTagName) return false;
            if(!document.getElementsByTagName("p")) return false;
            var elem = document.getElementsByTagName("p");
            elem.style.position = "absolute";
            elem.style.top = "500px";
            elem.style.left = "100px";
            
        }
        addLoadElement(positionMessage);
        
    </script>
</head>
<body>
    

666666

</body>

< / html >

what result do you expect? What is the error message actually seen?

problem description

error occurred [Web browser] "Uncaught TypeError: Cannot set property "position" of undefined" / HelloHBuilder/new_file.html (22)
I want to know what went wrong.

May.27,2021

getElementsByTagName returns an array, and your return false is useless.

var elem = document.getElementsByTagName("p")[0]

enter
document.getElementsByTagName ('p') in console, and then press enter. You can see that it is a HTMLCollection, which is a class array object, and that if judgment is useless, because a HTMLCollection, result is returned as true regardless of whether it is found or not.
you can get the DOM object you want in the form of an array subscript, or add class or id to get it instead.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ebdb74-4874a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ebdb74-4874a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?