导航
×
   ❮   
HTML CSS JavaScript PHP Go Sass W3C Colors ECMS

JS 参考手册

JS 参考手册(字母排序)

JavaScript

JS Arrays JS Boolean JS Classes JS Dates JS Error JS Global JS Iterators JS JSON JS Maps JS Math JS Numbers JS Objects JS Operators JS Assignment JS Arithmetic JS Comparison JS Logical Operators JS Bitwise Operators JS Misc Operators JS Precedence JS Promises JS RegExp Patterns JS RegExp Reference JS Sets JS Statements JS Strings JS Typed Arrays JS Typed Reference

Window

Window Object Window Console Window History Window Location Window Navigator Window Screen

HTML DOM

HTML Documents HTML Elements HTML Attributes HTML Collection HTML NodeList HTML DOMTokenList HTML Styles
alignContent alignItems alignSelf animation animationDelay animationDirection animationDuration animationFillMode animationIterationCount animationName animationTimingFunction animationPlayState background backgroundAttachment backgroundClip backgroundColor backgroundImage backgroundOrigin backgroundPosition backgroundRepeat backgroundSize backfaceVisibility border borderBottom borderBottomColor borderBottomLeftRadius borderBottomRightRadius borderBottomStyle borderBottomWidth borderCollapse borderColor borderImage borderImageOutset borderImageRepeat borderImageSlice borderImageSource borderImageWidth borderLeft borderLeftColor borderLeftStyle borderLeftWidth borderRadius borderRight borderRightColor borderRightStyle borderRightWidth borderSpacing borderStyle borderTop borderTopColor borderTopLeftRadius borderTopRightRadius borderTopStyle borderTopWidth borderWidth bottom boxShadow boxSizing captionSide caretColor clear clip color columnCount columnFill columnGap columnRule columnRuleColor columnRuleStyle columnRuleWidth columns columnSpan columnWidth counterIncrement counterReset cssFloat cursor direction display emptyCells filter flex flexBasis flexDirection flexFlow flexGrow flexShrink flexWrap font fontFamily fontSize fontStyle fontVariant fontWeight fontSizeAdjust height isolation justifyContent left letterSpacing lineHeight listStyle listStyleImage listStylePosition listStyleType margin marginBottom marginLeft marginRight marginTop maxHeight maxWidth minHeight minWidth objectFit objectPosition opacity order orphans outline outlineColor outlineOffset outlineStyle outlineWidth overflow overflowX overflowY padding paddingBottom paddingLeft paddingRight paddingTop pageBreakAfter pageBreakBefore pageBreakInside perspective perspectiveOrigin position quotes resize right scrollBehavior tableLayout tabSize textAlign textAlignLast textDecoration textDecorationColor textDecorationLine textDecorationStyle textIndent textOverflow textShadow textTransform top transform transformOrigin transformStyle transition transitionProperty transitionDuration transitionTimingFunction transitionDelay unicodeBidi userSelect verticalAlign visibility width wordBreak wordSpacing wordWrap widows zIndex

HTML Events

HTML Events HTML Event Objects HTML Event Properties HTML Event Methods

Web APIs

API Canvas API Console API Fetch API Fullscreen API Geolocation API History API MediaQueryList API Storage API Validation API Web

HTML 对象

<a> <abbr> <address> <area> <article> <aside> <audio> <b> <base> <bdo> <blockquote> <body> <br> <button> <canvas> <caption> <cite> <code> <col> <colgroup> <datalist> <dd> <del> <details> <dfn> <dialog> <div> <dl> <dt> <em> <embed> <fieldset> <figcaption> <figure> <footer> <form> <head> <header> <h1> - <h6> <hr> <html> <i> <iframe> <img> <ins> <input> button <input> checkbox <input> color <input> date <input> datetime <input> datetime-local <input> email <input> file <input> hidden <input> image <input> month <input> number <input> password <input> radio <input> range <input> reset <input> search <input> submit <input> text <input> time <input> url <input> week <kbd> <label> <legend> <li> <link> <map> <mark> <menu> <menuitem> <meta> <meter> <nav> <object> <ol> <optgroup> <option> <output> <p> <param> <pre> <progress> <q> <s> <samp> <script> <section> <select> <small> <source> <span> <strong> <sub> <summary> <sup> <table> <tbody> <td> <tfoot> <th> <thead> <tr> <textarea> <time> <title> <track> <u> <ul> <var> <video>

HTML DOM NodeList


NodeList

NodeList 节点列表是一个类似数组的节点对象集合(列表)。

可以通过索引(从 0 开始)访问节点列表中的节点。

length 属性 返回节点列表中的节点数。


NodeList 与 HTMLCollection

NodeList 几乎与 HTMLCollection 相同。

请参阅以下说明。



属性和方法

NodeList 可以使用以下属性和方法:

名称 描述
entries() 返回一个包含列表中键值对的迭代器
forEach() 对列表中的每个节点执行回调函数
item() 返回指定索引处的节点
keys() 返回一个包含列表中所有键的迭代器
length 返回 NodeList 中的节点数
values() 返回一个包含列表中所有值的迭代器

示例

选择文档中的所有<p>个节点:

const myNodeList = document.querySelectorAll("p");

可以通过索引号访问 NodeList 中的元素。

要访问第二个<p> 节点,您可以这样写:

myNodeList[1]
亲自试一试 »

注意:索引从 0 开始。


HTML DOM 节点列表长度

length属性定义了节点列表中的节点数量:

示例

myNodelist.length
亲自试一试 »

length 属性在需要遍历节点列表中的节点时非常有用:

示例

更改节点列表中所有<p> 元素的颜色:

const myNodelist = document.querySelectorAll("p");
for (let i = 0; i< myNodelist.length; i++) {
  myNodelist[i].style.color = "red";
}
亲自试一试 »

不是数组

NodeList 不是数组!

NodeList 看起来像数组,但它不是。

你可以遍历 NodeList 并使用索引访问其节点。

但是你不能对 NodeList 使用数组方法,例如 push()、pop() 或 join()。


HTMLCollection 和 NodeList 的区别

NodeList 和 HTMLCollection 本质上是相同的。

两者都是从文档中提取的节点(元素)的数组式集合(列表)。可以通过索引号访问节点。索引从 0 开始。

两者都有一个 length 属性,用于返回列表(集合)中的元素数量。

HTMLCollection 是一个包含 文档元素 的集合。

NodeList 是一个包含 文档节点(元素节点、属性节点和文本节点)的集合。

HTMLCollection 中的元素可以通过名称、ID 或索引号访问。

NodeList 中的元素只能通过索引号访问。

HTMLCollection 始终是动态集合。例如:如果您向 DOM 中的列表添加一个<li> 元素,HTMLCollection 中的列表也会随之改变。

NodeList 通常是一个静态集合。例如:如果您向 DOM 中的列表添加一个<li> 元素,NodeList 中的列表不会改变。

getElementsByClassName()getElementsByTagName() 方法返回一个动态 HTMLCollection 对象。

querySelectorAll() 方法返回一个静态 NodeList 对象。

childNodes 属性返回一个动态 NodeList 对象。

实时节点列表

在某些情况下,节点列表是实时的:DOM 的变化会更新节点列表。

childNodes 方法返回一个实时节点列表。


Copyright ©2020-2026 freew3c.com All Rights Reserved 提供的内容仅用于学习和测试,不保证内容的正确性。