DOM API 终极指南
// Selecting Elements: document is not the real DOM element. document.documentElement; // Select the entire page document.head; // Select the head document.body; // Select the body document.querySelector('.header'); // return first match const allSections = document.querySelectorAll('.section'); // return all-matches in a static NodeList document.getElementById('id-name'); // document.getElementsByClassName(''); // return all-matches in an live HTMLCollection const allBtns = document.getElementsByTagName('button'); // return all-matches in an live HTMLCollection // Creating & Inserting Elements: insertAdjacentHTML const msg = document.createElement('div'); // create a DOM element, stores it into msg msg.classList.add('cookie-msg'); // msg.textContent = 'We use cookies for improved functionality & analytics'; msg.innerHTML = 'We use cookies for improved functionality & analytics <button class="btn">Got it</button>'; header.append(msg); // prepend: Adds the element as the first child. // append: Adds the element as the last child. // DOM element is unique, it can exist at only one place at a time. // To insert multiple copies of the same element, true refers all childNodes will also be copied. header.append(msg.cloneNode(true)); header.before(msg); // insert before header element as a sibling. header.after(msg); // insert after header element as a sibling. // Delete Elements: document.querySelector will also work, but below is another way. // remove() is a recent method, earlier we could only remove child elements by selecting the parent element first, then removing the child. Ex msg.parentElement.removeChild(msg); document.querySelector('btn-close').addEventListener('click', function(){ msg.remove(); }); // Styles: will be applied as inline styles. msg.style.backgroundColor = '#37383d'; msg.style.width = '120%'; msg.style.height; // won't show anything. This works only for properties which we have explicitly set like the above properties. getComputedStyle(msg).color; // will show a huge object containing all styles. getComputedStyle(msg).height; // Increase height by 10px msg.style.height = Number.parseFloat(getCOmputedStyle(msg).height,10) + 40 + 'px'; document.documentElement.style.setProperty('--color-primary','orangered'); // Attributes const logo = document.querySelector('.nav__logo'); logo.alt; logo.className; // Setting an attribute using JS. logo.alt = 'Beautiful minimalist logo' // won't work as its not a standard attribute for that element. logo.designer; // Now it will work. logo.getAttribute('designer'); logo.setAttribute('company', 'Google'); logo.src; // absolute path logo.getAttribute('src'); // relative path // Both will be same as they are absolute in both cases. link.href; link.getAttribute('href'); // Data-attributes written in // HTML as data-version-number // JS as logo.dataset.versionNumber; // such special attributes are always stored in dataset object. // Classes logo.classList.add() // Can take multiple classes as args logo.classList.remove() // Can take multiple classes as args logo.classList.toggle() logo.classList.contains() // Overwrite all existing classes, replace with the bottom class mentioned. logo.className = 'xyz'
注释:
点击锚链接将使页面跳转到顶部。使用 e.preventDefault()
可以防止这种情况
NodeList 不是数组,但它有 forEach 方法。
例如 btnOpenModal.forEach(btn => btn.addEventListener('click', openModal));
以上是DOM API 终极指南的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

本文讨论了在浏览器中优化JavaScript性能的策略,重点是减少执行时间并最大程度地减少对页面负载速度的影响。

本文讨论了使用浏览器开发人员工具的有效JavaScript调试,专注于设置断点,使用控制台和分析性能。

Python和JavaScript开发者的薪资没有绝对的高低,具体取决于技能和行业需求。1.Python在数据科学和机器学习领域可能薪资更高。2.JavaScript在前端和全栈开发中需求大,薪资也可观。3.影响因素包括经验、地理位置、公司规模和特定技能。

本文说明了如何使用源地图通过将其映射回原始代码来调试JAVASCRIPT。它讨论了启用源地图,设置断点以及使用Chrome DevTools和WebPack之类的工具。

深入探讨console.log输出差异的根源本文将分析一段代码中console.log函数输出结果的差异,并解释其背后的原因。�...

掌握了入门级TypeScript教程后,您应该能够在支持TypeScript的IDE中编写自己的代码,并将其编译成JavaScript。本教程将深入探讨TypeScript中各种数据类型。 JavaScript拥有七种数据类型:Null、Undefined、Boolean、Number、String、Symbol(ES6引入)和Object。TypeScript在此基础上定义了更多类型,本教程将详细介绍所有这些类型。 Null数据类型 与JavaScript一样,TypeScript中的null
