04-01 浏览器对象模型BOM - 图1


JS的DOM操作

我们已经知道了css选择器本质就是css与html两种语法建立关联的特定标识符,那在JS语言的语法中,也有特点的方式与html语言编写的表情建立关联,我们就称之为JS选择器。

1、getElement系列

  1. // 1.通过id名获取唯一满足条件的页面元素
  2. document.getElementById('id名');
  3. // 该方法只能由document调用
  4. // 2、通过class名获取所有满足条件的页面元素
  5. document.getElementsByClassName('class名');
  6. // 该方法可以由document及任意页面元素对象调用
  7. // 返回值为HTMLCollection (一个类数组结果的对象,使用方式同数组)
  8. // 没有匹配到任何结果返回空HTMLCollection对象 ([])
  9. // 3.通过tag名获取所有满足条件的页面元素
  10. document.getElementsByTagName('tag名');
  11. // 该方法可以由document及任意页面元素对象调用
  12. // 返回值为HTMLCollection (一个类数组结果的对象,使用方式同数组)
  13. // 没有匹配到任何结果返回空HTMLCollection对象 ([])

2、querySelect系列

  1. // 1.获取第一个匹配到的页面元素
  2. document.querySelector('css3语法选择器');
  3. // 该方法可以由document及任意页面对象调用
  4. // 2.获取所有匹配到的页面元素
  5. document.querySelectorAll('css3语法选择器');
  6. // 该方法可以由document及任意页面对象调用
  7. // 返回值为NodeList (一个类数组结果的对象,使用方式同数组)
  8. // 没有匹配到任何结果返回空NodeList对象 ([])

2-1 案例:

  1. <body>
  2. <div id="box" class="box"></div>
  3. <script>
  4. var box1 = document.getElementById('box');
  5. var box2 = document.querySelector('.box');
  6. // box1 === box2,就代表页面idboxclassboxdiv标签
  7. </script>
  8. </body>

3、JS页面操作

我们学习完JS的基本语法后,知道如何简单使用JS语言,有掌握了JS选择器,就可以与页面建立起联系,那我们可以通过哪些方式与页面标记进行交互?有可以在交互的过程中对标签进行哪些具体的操作呢?

3-1 鼠标事件

我们先来看一下交互的方式,叫做标签对象的事件绑定,可以绑定的事件有:

  1. /*
  2. onclick:鼠标点击
  3. ondblclick:鼠标双击
  4. onmousedown:鼠标按下
  5. onmousemove:鼠标移动
  6. onmouseup:鼠标抬起
  7. onmouseover:鼠标悬浮
  8. onmouseout:鼠标移开
  9. oncontextmenu:鼠标右键
  10. */

3-2 事件的绑定

具体绑定事件的方式:

  1. <body>
  2. <div class="box">绑定点击事件后可以完成点击交互</div>
  3. <script>
  4. var box = document.querySelector('.box');
  5. // 页面classboxdiv被鼠标点击后会有弹出框
  6. box.onclick = function() {
  7. alert("box标签被点击了")
  8. }
  9. </script>
  10. </body>

那么绑定完事件后又可以怎样具体操作页面标签呢?

3-3 操作行间式样式

  1. <head>
  2. <style>
  3. .box {
  4. width: 200px;
  5. height: 200px;
  6. }
  7. </style>
  8. </head>
  9. <body>
  10. <div class="box" style="background-color: red"></div>
  11. <script>
  12. var box = document.querySelector('.box');
  13. // 语法:页面对象.全局style属性.具体的样式名
  14. box.onclick = function() {
  15. // 读:获取行间式样式值
  16. var bgColor = box.style.backgroundColor;
  17. // 写:对行间式样式进行赋值,初始没有该条行间式样式,相同会自动添加设置好的行间式
  18. box.style.backgroundColor = 'orange'; // css3多个单词的属性名采用小驼峰命名法
  19. }
  20. </script>
  21. </body>

3-4 只读 计算后 样式

  1. <head>
  2. <style>
  3. .box {
  4. width: 200px;
  5. height: 200px;
  6. }
  7. </style>
  8. </head>
  9. <body>
  10. <div class="box" style="background-color: red"></div>
  11. <script>
  12. var box = document.querySelector('.box');
  13. // 语法:getComputedStyle(页面元素对象, 伪类).样式名;
  14. // 注:我们不需要考虑伪类的范畴,直接用null填充即可
  15. box.onclick = function() {
  16. // 只读:获取计算后样式值
  17. var width = getComputedStyle(box, null).width;
  18. }
  19. </script>
  20. </body>

3-5 操作标签class名

  1. <body>
  2. <div class="box">class名操作</div>
  3. <script>
  4. var box = document.querySelector('.box');
  5. // 查看类名
  6. var cName = box.className;
  7. // 修改类名
  8. box.className = "ele";
  9. // 增加类名
  10. box.className = " tag"; // 添加后的结果class="ele tag",所以赋值时一定注意tag前有个空格字符串
  11. // 删除所有类名
  12. box.className = "";
  13. </script>
  14. </body>

3-6 操作标签全局属性值

  1. <body>
  2. <img src="https://www.baidu.com/favicon.ico" class="image" />
  3. <script>
  4. var img = document.querySelector('.image');
  5. // 查看全局属性值
  6. var imgSrc = img.getAttribute('src');
  7. // 修改全局属性值
  8. img.setAttribute('src', 'img/bg_logo.png');
  9. // 删除全局属性值
  10. img.setAttribute = ('src', '');;
  11. </script>
  12. </body>