js代码位置问题
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" type="text/css" href="css/css.css"> <script type="text/javascript"> console.log(document.getElementsByClassName("nav-item")); </script> <!-- <script src="js/js.js"></script> --> </head> <body> <div class="main"> <!-- 导航 --> <div class="nav" id="nav"> <div class="nav-item actived"> <a href="javascript:void(0)"> <span>首页</span> </a> </div> <div class="nav-item"> <a href="javascript:void(0)"> <span>点击看看</span> </a> </div> <div class="nav-item"> <a href="javascript:void(0)"> <span>会自动的</span> </a> </div> <div class="nav-item"> <a href="javascript:void(0)"> <span>我的网站</span> </a> </div> </div> <!-- 轮播图 --> <div class="slide-pics" id="pics"> <div class="pic-item actived"> <img src="img/1.jpg" alt=""> </div> <div class="pic-item"> <img src="img/2.jpg" alt=""> </div> <div class="pic-item"> <img src="img/3.jpg" alt=""> </div> <div class="pic-item"> <img src="img/4.jpg" alt=""> </div> </div> </div> </body> </html>
之前onload那节课不是说这样会报错吗?为什么我这边能取出元素?
0
收起
正在回答
3回答
document.getElementsByClassName("")返回的是一个引用类型,指向的是一个地址,所以浏览器是可以显示出来的,
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" type="text/css" href="css/css.css"> <script type="text/javascript"> console.log(document.getElementsByClassName("nav-item")[0]); </script> <!-- <script src="js/js.js"></script> --> </head> <body> <div class="main"> <!-- 导航 --> <div class="nav" id="nav"> <div class="nav-item actived"> <a href="javascript:void(0)"> <span>首页</span> </a> </div> <div class="nav-item"> <a href="javascript:void(0)"> <span>点击看看</span> </a> </div> <div class="nav-item"> <a href="javascript:void(0)"> <span>会自动的</span> </a> </div> <div class="nav-item"> <a href="javascript:void(0)"> <span>我的网站</span> </a> </div> </div> <!-- 轮播图 --> <div class="slide-pics" id="pics"> <div class="pic-item actived"> <img src="img/1.jpg" alt=""> </div> <div class="pic-item"> <img src="img/2.jpg" alt=""> </div> <div class="pic-item"> <img src="img/3.jpg" alt=""> </div> <div class="pic-item"> <img src="img/4.jpg" alt=""> </div> </div> </div> </body> </html>
同学可以将代码替换为console.log(document.getElementsByClassName("nav-item")[0]);进行测试哦~
祝学习愉快!
前端小白入门系列课程
- 参与学习 人
- 提交作业 11218 份
- 解答问题 36713 个
从一个不会编程的小白到一个老司机是需要过程的,首先得入门,学习基础知识,然后才能进阶,最后再到精通,本专题是你走进前端世界的不二选择!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星