JS(Dom对象的属性和方法)第十六课

news2024/10/7 14:29:56
Dom对象的属性和方法

 

自定义的熟悉操作

 

上面是今天博客要讲述的内容

一个案例回顾上次课讲的内容  下面是Html中的元素布局结构

  <div>我是div审查元素
      <p>我是p标记的元素</p>
      <span>我是span的元素信息</span>
  </div>
  <div class="one">
      <p class="two">我是class选择器</p>
      <p id="three">我是第二个元素</p>
      <p>段落标签</p>
      <from>
          <table>
              <tr>
                  <td>姓名</td>
                  <td><input type="text" name="name" id="" placeholder="请用户输入姓名"></td>
              </tr>
              <tr>
                  <td>密码</td>
                  <td><input type="password" name="password" id="" placeholder="请用户输入密码"></td>
              </tr>
          </table>
      </from>
  </div>
  <div class="two">
      <p class="two">我是第三个节点</p>
      <p id="two">我是第二个元素</p>
      <p>
      <ul id="tabelList">
          <li>我是标签li</li>
          <li>我是标签精准定位获取的元素信息</li>
          <li>我是标签li</li>
          <li>我是标签li</li>
          <li>我是标签li</li>
          <li>我是标签li</li>
          <li>我是标签li</li>
          <li>我是标签li</li>
          <li>我是标签li</li>
          <li>我是标签li</li>
      </ul>
      </p>
  </div>

下面的基本操作是对上次课的节点操作的回顾 注释在文件中 不在过多的介绍 

  <script>
      // 文档声明
      var DOCTYPE = document.doctype
      console.log(DOCTYPE)
      // html
      var html = document.documentElement
      console.log(html);
      html.style.background = 'pink'
      // head
      var head = document.head
      console.log(head)
      head.style.background = "yellow"
      // body
      var body = document.body
      console.log(body)
      body.style.background = 'pink'
      // head的子节点
      console.log("获得head的子节点的理解")
      var head = document.head
      console.log(head)
      var one = document.head.children[0]
      console.log(one)
      console.log(document.head.children[1])
      console.log(document.head.children[2])
      console.log(document.head.children[3])
      console.log(document.head.children[4])
      console.log(document.head.children[5])
      
  </script>
// 兄弟节点 
      // document.head.nextElementSibling 获取head的后一个节点元素为body
      console.log(document.head.nextElementSibling)
      // document.body.previousElementSibling 获取body的前兄弟节点是 head
      console.log(document.body.previousElementSibling)
      // 父节点
      console.log("获取父元素的节点")
      console.log(document.head.parentNode)
      console.log(document.body.parentNode)
      // 总结 获取document.head.parentNode)和document.body.parentNode 总节点为Html
      // console.log(document.html.parentNode) 报错
      console.log("____________________________________________________________________________________________")
      console.log("获取元素的父节点")
      console.log(document.body.parentElement)
      console.log(document.head.parentElement)
      // 总结 获取document.head.parentElement)和document.body.parentElement总节点为Html
     
 // document.head.nextElementSibling 获取head的后一个节点元素为body
      console.log(document.head.nextElementSibling)
      // document.body.previousElementSibling 获取body的前兄弟节点是 head
      console.log(document.body.previousElementSibling)
      var two = document.body.children[0]
      console.log(two)
      console.log(document.body.children[1])
      console.log(document.body.children[2])
      console.log(document.body.children[3])
      // 第一个
      console.log(document.body.firstChild)
      console.log(document.body.lastChild)
      console.log("操作Dom对象的属性和方法")
      

 

 一个基础案例 带你了解 Dom对象的属性和方法

 <div class="box">盒子1</div>
 <div class="box">盒子2</div>
 <div id="nav">
     <ul>
         <li>首页</li>
         <li>产品</li>
         <li class="nav_li">项目名称</li>
     </ul>
 </div>
带你了解 Dom对象的属性和方法

 

  // 1. getElementsByClassName 根据类名获得某些元素集合
  var boxs = document.getElementsByClassName('box');
  console.log(boxs);
  var nav_li=document.getElementsByClassName('nav_li')
  console.log(nav_li)
  // 2. querySelector 返回指定选择器的第一个元素对象  切记 里面的选择器需要加符号 .box  #nav
  var firstBox = document.querySelector('.box');
  console.log(firstBox);
  var nav = document.querySelector('#nav');
  console.log(nav);
  var li = document.querySelector('li');
  console.log(li);
  // 3. querySelectorAll()返回指定选择器的所有元素对象集合
  var allBox = document.querySelectorAll('.box');
  console.log(allBox);
  var lis = document.querySelectorAll('li');
  console.log(lis);
在上面的6个方法中最常用的是上面的两个方法

 

// 5.querySelector() — 精准的获取某个元素
var e = document.querySelector("#tabelList li:nth-child(2)")
console.log(e)

// 6.querySelectorAll()获取符合类名或者标签名等条件的的所有元素
var f = document.querySelectorAll('div')
console.log(f)

自定义属性操作 语法如下

自定义的熟悉操作

自定义属性操作 运行效果如上所示

 

 <div age="12" ac="" wer class="box" id="div1"></div>

 var div1 = document.querySelector("#div1") //通过document.querySelector("#div1") 获取div1盒子
 var flag = div1.hasAttribute("age")       //判断属性是否存在
 var attr = div1.getAttribute("age")  //获取属性的值
 var srt = div1.setAttribute("name", "tyu")  //设置属性值
 var ligt = div1.getAttribute("name")  //删除属性值没有返回值
 // var liko = div1.removeAttribute("ligt") //获取所有的属性的内容
 console.log(div1)
 console.log(ligt) 
  console.log(liko)

 var attrs=div1.attributes  //获取所有的属性
 console.log(attrs)
 for(var item of attrs){
     console.log(item)
 }
 // console.log(flag)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1289.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

搭建ftp服务器注意事项

很早之前写过一篇关于FTP服务器相关博客&#xff1a;FTP服务器主动被动模式详解。直到最近生产环境需要搭建一个FTP服务&#xff0c;才发现一些细节还需注意。 生产环境搭建ftp服务器&#xff0c;如果有外部用户需要连接ftp服务器下载文件&#xff0c;一般用的是Passive (PASV&…

[深度学习] 名词解释--正则化

正则化 花书的定义&#xff1a;凡是可以减少泛化误差&#xff08;过拟合&#xff09;而不是减少训练误差的方法&#xff0c;都叫正则化方法。 目的&#xff1a;拟合训练数据&#xff0c;防止模型过拟合&#xff0c;通常使用L2正则化.用各种方法规范模型参数的方法. 什么是神经网…

408 | 【2012年】计算机统考真题 自用回顾知识点整理

选择题 T2&#xff1a;后缀表达式&#xff08;逆波兰表达式&#xff09;—— 注意 操作数的顺序&#xff01; 中缀转后缀 运算顺序不唯一&#xff0c;因此对应的后缀表达式也不唯一“左优先”原则&#xff1a;只要左边的运算符能先计算&#xff0c;就优先算左边的用栈实现中缀…

JS(DOM)第十五课

Dom的全称是Document Object Model DOM 定义了访问 HTML 和 XML 文档的标准 Dom的定义 DOM 文档对象模型 DOM是针对XML的基于树的API。描述了处理网页内容的方法和接口&#xff0c;是HTML和XML的API&#xff0c;DOM把整个页面规划成由节点层级构成的文档。 DOM本身是与语言无…

基于simulink的超级电容,电池及DC motor充放电系统仿真

目录 一、理论基础 二、核心程序 三、测试结果 作者ID &#xff1a;fpga和matlab CSDN主页&#xff1a;https://blog.csdn.net/ccsss22?typeblog 擅长技术&#xff1a; 1.无线基带,无线图传,编解码 2.机器视觉,图像处理,三维重建 3.人工智能,深度学习 4.智能控制,智能优…

【C ++基础】迭代器(iterator)在string里面的简单使用

【C 基础】迭代器(iterator)在string里面的简单使用 前言 本文是为了扫清后面学习的难点&#xff0c;而特意写的文章&#xff0c;只是介绍迭代器如何在string中使用。 迭代器的详细解释请看这里&#xff1a; [点击跳转&#xff08;这里还没有写哦&#xff09;] C专栏主页&am…

【Node.JS】事件的绑定与触发

往期文章 【Node.JS】写入文件内容 【Node.JS】读取文件内容 目录 简介 绑定事件 on&#xff08;&#xff09; addListener&#xff08;&#xff09; once&#xff08;&#xff09; 监听事件emit&#xff08;&#xff09; 传参 删除事件 removeListener&#xff08;&am…

【无人机】基于蚁群算法的无人机航路规划研究附matlab代码

✅作者简介&#xff1a;热爱科研的Matlab仿真开发者&#xff0c;修心和技术同步精进&#xff0c;matlab项目合作可私信。 &#x1f34e;个人主页&#xff1a;Matlab科研工作室 &#x1f34a;个人信条&#xff1a;格物致知。 更多Matlab仿真内容点击&#x1f447; 智能优化算法 …

【笔试题】【day14】

目录 第一题&#xff08;哪些成员变量需要在初始化列表初始化&#xff09; 第二题&#xff08;多态的条件&#xff09; 第三题&#xff08;多态的条件&#xff08;通过父类对象调用的全部都是父类的方法&#xff09;&#xff09; 第四题&#xff08;菱形继承&#xff0c;虚…

猴子也能学会的jQuery第一期——什么是jQuery

#1024程序员节&#xff5c;用代码&#xff0c;改变世界 &#x1f4da;系列文章—目录&#x1f525; 猴子也能学会的jQuery第一期——什么是jQuery 猴子也能学会的jQuery第二期——引入jQuery 未完待续 目录 &#x1f4da;系列文章—目录&#x1f525; &#x1f4c4;需要具备的…

【最佳实践】瀚高数据库 Windows企业版v6.0.4 的安装

Windows7、Windows10 一、安装 解压缩文件“hgdb6.0.4-enterprise-windows10-x86-64-20220127.zip”&#xff0c;以【管理员】身份运行 setup.exe。 安装过程基本上是下一步下一步即可&#xff0c;注意步骤如下。 1.1 安装主目录 注意安装路径&#xff0c;不要包含中文&am…

【创作中心】自定义模板的使用

个人主页&#xff1a;天寒雨落的博客_CSDN博客-初学者入门C语言,数据库,python领域博主 &#x1f4ac; 刷题网站&#xff1a;一款立志于C语言的题库网站蓝桥杯ACM训练系统 - C语言网 (dotcpp.com) 特别标注&#xff1a;该博主将长期更新c语言内容&#xff0c;初学c语言的友友们…

2022年12月1日起,OCS将停止接受GOTS的原料投入

【2022年12月1日起&#xff0c;OCS将停止接受GOTS的原料投入】 GOTS介绍&#xff1b; 全球有机纺织品标准&#xff0c;Global Organic Textile Standard, 简称GOTS。该标准由国际天然纺织品协会&#xff08;IVN&#xff09;、日本有机棉协会&#xff08;JOCA&#xff09;&#…

数字化智慧梁场管理系统解决方案

系统介绍 数字化智慧梁场管理系统实现对预制构件生产的全周期智慧化管理。系统以BIM为基础&#xff0c;搭建完整数字孪生模型&#xff0c;依据生产工序智能排程&#xff1b;围绕生产工序&#xff0c;生产任务智能传递&#xff0c;协同工作&#xff1b;依据生产工序&#xff0c;…

Python实战:获取bing必应壁纸首页的每日一图(仅做学习用)

目录需求网站分析代码实现进一步接口获取其他资源需求 老板&#xff1a;微软必应https://cn.bing.com/ 首页的每日一图看着不错&#xff0c;能不能自动获取 我&#xff1a;我试试 网站分析 我们查看网页元素&#xff0c;不难发现背景图就在类名为.img_cont 的标签下 可是搜…

Vitepress搭建组件库文档(下)—— 组件 Demo

上文 《Vitepress搭建组件库文档&#xff08;上&#xff09;—— 基本配置》已经讨论了 vitepress 搭建组件库文档的基本配置&#xff0c;包括站点 Logo、名称、首页 home 布局、顶部导航、左侧导航等。本文进入最重要的部分 —— 如何像 Element Plus 那样一遍代码就可以展示组…

面试查漏补缺--java基础-容器源码解读

前言&#xff1a; 本文主要是通过源码来解读一些自己还不懂的地方&#xff0c;一些数据结构上的东西&#xff0c;不做过多的解读。 文章目录一、容器体系二、List容器2.1 ArrayList源码2.2 Vector 源码2.3 LinkedList三、Set容器3.1 HashSet一、容器体系 容器总的来说分为两大…

小小博客项目(servlet实战演练)

目录 MVC模式简介 项目概述 &#x1f351;Model&#xff08;模型层&#xff09; &#x1f351;View&#xff08;视图层&#xff09; &#x1f351;Controller&#xff08;控制器层) 项目实战 上面pom.xml代码参考 一、模型层 &#x1f330;User代码&#xff1a;对应数据…

什么是NFS?NFS挂载

文章目录1、NFS服务2、RPC服务与NFS3、NFS的优缺点4、NFS服务端的搭建与配置5、小结1、NFS服务 NFS&#xff0c;全称Network File System&#xff0c;即网络文件系统。最大的功能是通过网络&#xff0c;让不同的机器、不同的操作系统可以共享彼此的文件。 &#x1f609; 更直白…

Go语言开发k8s-05-ConfigMap操作

文章目录1. 结构体1.1 ConfigMapList1.2 ConfigMap1.3 TypeMeta1.4 ObjectMeta1.7 对照yml文件示例1.5 Immutable1.6 Data1.7 BinaryData2. Create configMap语法完整示例3. Get ConfigMapList语法完整示例4. Get ConfigMap语法完整示例5. Update ConfigMap语法完整示例6. Dele…