简体   繁体   English

为什么控制台给我结果未定义

[英]Why console giving me result undefined

I Don't khow why my console is telling me that the result is undefined.I am learning DOM and found the problem on my first code which i don't understand我不知道为什么我的控制台告诉我结果未定义。我正在学习 DOM 并在我不明白的第一个代码中发现了问题

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> let head = document.getElementsByClassName(".main"); console.log(head.textContent); let tail = document.getElementsByTagName("p"); console.log(tail.textContent); </script> </head> <body> <div class="main"> <p>hello</p> </div> </body> </html>

  1. The elements doesn´t exist when the script runs in head.当脚本在头部运行时,元素不存在。 Move the script to after the elements将脚本移动到元素之后
  2. Change from .main to main in getElementsByClassNamegetElementsByClassName.main更改为main
  3. getElementsByClassName returns a list so add [0] to getElementsByClassName("main") to get the first element getElementsByClassName返回一个列表,因此将[0]添加到getElementsByClassName("main")以获取第一个元素

 <div class="main"> <p>hello</p> </div> <script> let head = document.getElementsByClassName("main")[0]; console.log(head.textContent); let tail = document.getElementsByTagName("p")[0]; console.log(tail.textContent); </script>

  • Move your script to the body将您的脚本移至正文
  • document.getElementsByClassName returns a collection of HTML elements, not a single element document.getElementsByClassName返回 HTML 元素的集合,而不是单个元素
  • with document.getElementsByClassName you should pass main , not .main使用document.getElementsByClassName你应该通过main ,而不是.main
  • the textContent of the div with class main is probably not what you expectmain的 div 的 textContent 可能不是您期望的
  • to get an single element by its class name or its tag name, you can also use querySelector which is more simple (but slower) Here is a working version :要通过其类名或标签名获取单个元素,您还可以使用更简单(但速度较慢)的querySelector这是一个工作版本:
<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>

    <body>
        <div class="main">
            <p>hello</p>
        </div>
  
        <script>
            let head = document.querySelector(".main");
            console.log(head.textContent);
            let tail = document.querySelector("p");
            console.log(tail.textContent);
        </script>
    </body>

</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么这个JavaScript代码给我错误“未定义”? - Why is this JavaScript code giving me error “undefined”? 为什么 return 关键字没有给我想要的结果? - Why is the return keyword not giving me the desired result? 为什么在定义startsWith时,控制台给我一个例外? - why is the console giving me an exception, when startsWith is defined? 事件没有给我结果 - event is not giving me the result 为什么我的键码测试在画布游戏中给我未定义的? - Why is my keycode test giving me undefined in canvas game? 为什么Grunt Copy给我一个“未定义不是函数”警告? - Why is Grunt Copy giving me an 'undefined is not a function' warning? 为什么 MUI CORE 基本评级代码给我的 setValue 未定义? - why is MUI CORE basic rating code is giving me setValue as undefined? 为什么这个输入值总是给我未定义的? - Why does this inputs value keep giving me undefined? 为什么我的排列算法对所有排列都给出相同的结果? - Why is my permutation algorithm giving me the same result for all permutations? 为什么 VS 没有给我与 DevTools 中的控制台相同的结果? - Why VS doesn't give me the same result as console in DevTools?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM