当前位置: 首页 > news >正文

【HTML】document api

🔹 一、文档节点获取

  • 根节点相关

    • document.documentElement<html> 元素
    • document.head<head> 元素
    • document.body<body> 元素
  • 元素获取

    • getElementById(id) → 返回单个元素
    • getElementsByClassName(className) → 类数组,HTMLCollection
    • getElementsByTagName(tag) → 类数组,HTMLCollection
    • querySelector(selector) → 返回匹配的第一个元素
    • querySelectorAll(selector) → 返回 NodeList(可迭代)

🔹 二、节点创建 / 修改

  • createElement(tagName) → 创建元素节点
  • createTextNode(text) → 创建文本节点
  • appendChild(node) → 插入子节点
  • removeChild(node) → 移除子节点

🔹 三、文档信息

  • document.title → 文档标题
  • document.URL → 当前页面完整 URL
  • document.domain → 当前域名(可设置用于子域共享)
  • document.referrer → 引用来源 URL
  • document.cookie → 读写 cookie(受 SameSite、HttpOnly 限制)

🔹 四、事件相关

  • addEventListener(type, listener) → 事件绑定
  • removeEventListener(type, listener) → 移除事件
  • load 事件 → 所有资源加载完成

✅ 总结(面试答法)

👉 可以这样回答:

Document 提供的 API 主要分几类:

  1. 获取元素:如 getElementByIdquerySelector
  2. 创建修改:如 createElementappendChild
  3. 文档信息:如 titleURLcookie
  4. 事件相关:如 addEventListenerload

获取页面所有标签名

方法一:getElementsByTagName("*")

const allTags = document.getElementsByTagName("*")
const tagNames = [...allTags].map(el => el.tagName.toLowerCase())
console.log(tagNames)
  • * 表示选择所有元素
  • 返回 HTMLCollection,需要转为数组再取 .tagName
  • 输出所有标签名(可能会重复)

方法二:querySelectorAll("*")

const allTags = document.querySelectorAll("*")
const tagNames = [...allTags].map(el => el.tagName.toLowerCase())
console.log(tagNames)
  • 返回 NodeList,可直接用扩展运算符转数组
  • 效果和方法一类似

http://www.dtcms.com/a/337521.html

相关文章:

  • 【每天学点‘音视频’】前向纠错 和 漏包重传
  • 图像分类精度评价的方法——误差矩阵、总体精度、用户精度、生产者精度、Kappa 系数
  • 在 PyCharm Notebook 中安装 YOLO
  • Google 的 Opal:重新定义自动化的 AI 平台
  • 【项目】分布式Json-RPC框架 - 项目介绍与前置知识准备
  • ARM架构下的cache transient allocation hint以及SMMUv2的TRANSIENTCFG配置详解
  • kafka 冲突解决 kafka安装
  • 如何解决pip安装报错ModuleNotFoundError: No module named ‘pygame’问题
  • 数据赋能(401)——大数据——持续学习与优化原则
  • 删除并获得点数
  • 线程间通信(互斥锁,死锁,信号量)
  • 148-基于Python的2024物流年度销售收入数据可视化分析系统
  • PYTHON让繁琐的工作自动化-函数
  • 功能测试相关问题
  • 使用空模型实例调用辅助函数,确定在量化过程中哪些层会被跳过(43)
  • 实现make/makefile
  • Android RxBinding 使用指南:响应式UI编程利器
  • AI智能的“进化史”:从弱人工智能到通用人工智能的跨越
  • Linux中基于Centos7使用lamp架构搭建个人论坛(wordpress)
  • [Oracle数据库] Oracle 进阶应用
  • 【完整源码+数据集+部署教程】织物缺陷检测系统源码和数据集:改进yolo11-RevCol
  • 51单片机-驱动74HC595芯片实现IO口扩展模块教程
  • C++STL之list详解
  • MySQL 运算符详解:逻辑、位运算与正则表达式应用
  • CSS:水平垂直居中
  • 蔬菜批发小程序:生产商的数字化转型利器——仙盟创梦IDE
  • 吴恩达 Machine Learning(Class 1)
  • Fluss:颠覆Kafka的面向分析的实时流存储
  • 深入解析Kafka消费者重平衡机制与性能优化实践指南
  • 【Java基础】反射,注解,异常,Java8新特性,object类-详细介绍