此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Document:anchors 属性

Deprecated

Avoid using this feature in new projects. This feature may be a candidate for removal from web standards or browsers.

Document 接口的 anchors 只读属性返回文档中所有锚点的列表。

HTMLCollection

示例

js
if (document.anchors.length >= 5) {
  dump("发现了许多锚点");
}

以下是一个示例,它可以自动将页面上的每个锚点填充到目录中:

html
<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <title>测试</title>
    <script>
      function init() {
        const toc = document.getElementById("toc");
        for (const anchor of document.anchors) {
          const li = document.createElement("li");
          const newAnchor = document.createElement("a");
          newAnchor.href = "#" + anchor.name;
          newAnchor.textContent = anchor.text;
          li.appendChild(newAnchor);
          toc.appendChild(li);
        }
      }
    </script>
  </head>
  <body onload="init()">
    <h1>标题</h1>
    <h2><a name="contents">内容</a></h2>
    <ul id="toc"></ul>

    <h2><a name="plants">植物</a></h2>
    <ol>
      <li>苹果</li>
      <li>橙子</li>
      <li>梨</li>
    </ol>

    <h2><a name="veggies">蔬菜</a></h2>
    <ol>
      <li>胡萝卜</li>
      <li>西芹</li>
      <li>节奏</li>
    </ol>
  </body>
</html>

在 JSFiddle 中查看

备注

出于向后兼容性的原因,返回的锚点集合只包含使用 name 属性创建的锚点,而不包含使用 id 属性创建的锚点。

规范

规范
HTML
# dom-document-anchors

浏览器兼容性