<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <h1 class="abc">helloworld</h1>
        <h1 id="aa">nihao666</h1>
        <script type="text/javascript">
            //获取文档里面的具体某一个元素,通过css的标签、类、id选择可以选中元素,如果有多个元素,都被选择器选上,那么实际上只会选中最前面的元素
            var h1 = document.querySelector('h1')
            console.log(h1)
            
            
            //想选中多个元素
            var h1List = document.querySelectorAll('h1')
            console.log(h1List)
            
//            //document.queryselector,document.queryselectorAll都是ES5支持,es5标准发布时间是2008年,JQUERY库有类似于这样的功能,因为大家一致认同这样选择元素的模式,所以逼迫浏览器开发人员加上这样的功能
//            
//        
//            
//            //通过标签获取元素对象
            var h1bytag = document.getElementsByTagName('h1')
            console.log(h1bytag)
//            
//            
//            //通过样式类获取元素对象
            var h1byclass = document.getElementsByClassName('abc')
            console.log(h1byclass)
//            
//            //通过ID获取元素对象
//            var h1byid = document.getElementById('aa')
//            console.log(h1byid)
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        </script>
    </body>
</html>

 

相关文章:

  • 2022-01-08
  • 2022-12-23
  • 2021-11-01
  • 2021-10-03
  • 2022-01-23
  • 2021-09-21
  • 2021-12-11
  • 2021-08-07
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
  • 2022-12-23
  • 2021-12-17
  • 2021-03-31
  • 2021-11-08
相关资源
相似解决方案