HTML - DOM 文档 links 属性



HTML DOM document 的 **links** 属性是一个只读属性,它返回所有对应于 **<a>** 和 **<area>** 元素(带有 href 属性)的链接集合。

语法

document.links;

属性

属性 描述
length 它返回 HTML 文档中存在的 <a> 和 <area> 元素的数量。

方法

下表显示了 links 集合提供的方法列表。集合中的元素已排序,并按其在 HTML 文档中出现的顺序呈现。

方法 描述
[index] 返回集合中给定索引处的元素。索引从 0 开始,如果索引超出范围,则返回 null。
item(index) 返回集合中给定索引处的元素。索引从 0 开始,如果索引超出范围,则返回 null。它与第一种方法类似。
namedItem(id) 返回集合中具有给定 id 的元素。如果 id 不存在,则返回 null。

返回值

它返回一个 HTMLCollection 对象,其中列出了文档中所有存在的 **<a>** 和 **<area>** 元素。

HTML DOM 文档 'links' 属性示例

以下示例说明了 links 属性和方法的使用。

获取 <a> 和 <area> 元素的数量

在以下示例中,使用 **length** 属性返回文档中 <a> 和 <area> 元素的数量。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM document links Property
    </title>
</head>
<body>
    <a href="/index.htm">
        Welcome to Tutorials Point
    </a>
    <br><br>
    <area shape="rect" coords="" href="" alt="">
    <area shape="rect" coords="" href="" alt="">
    <a href=""></a>
    <button onclick="fun()">Click me</button>
    <p id="link"></p>
    <script>
        function fun() {
            let x = document.links.length;
            document.getElementById("link").innerHTML =x;
        }
    </script>
</body>
</html>

获取第一个 <a> 或 <area> 元素的 URL

在此示例中,我们使用 images **[index]** 方法获取第一个 <a> 或 <area> 元素的 URL。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM document links Property
    </title>
</head>
<body>
    <a href="/index.htm">
        Welcome to Tutorials Point
    </a>
    <br><br>
    <area shape="rect" coords="" href="" alt="">
    <area shape="rect" coords="" href="" alt="">
    <a href=""></a>
    <button onclick="fun()">Click me</button>
    <p id="links"></p>
    <script>
        function fun() {
            let x = document.links[0];
            document.getElementById("links").innerHTML = x;
        }
    </script>
</body>
</html>

获取 <a> 或 <area> 元素的 URL

在此示例中,我们使用 images **item(index)** 方法获取第一个 <a> 或 <area> 元素的 URL。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM document links Property
    </title>
</head>
<body>
    <a href="/index.htm">
        Welcome to Tutorials Point
    </a>
    <br><br>
    <area shape="rect" coords="" href="" alt="">
    <area shape="rect" coords="" href="" alt="">
    <a href=""></a>
    <button onclick="fun()">Click me</button>
    <p id="links"></p>
    <script>
        function fun() {
            let x = document.links.item(0);
            document.getElementById("links").innerHTML = x;
        }
    </script>
</body>
</html>

获取具有指定 id 的 <a> 或 <area> 元素的 URL

以下示例返回具有指定 id 的 <a> 或 <area> 元素的 URL。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM document links Property
    </title>
</head>
<body>
    <a href="/index.htm" id="link1">
        Welcome to Tutorials Point
    </a>
    <br><br>
    <area shape="rect" coords="" href="" alt="">
    <area shape="rect" coords="" href="" alt="">
    <a href=""></a>
    <button onclick="fun()">Click me</button>
    <p id="links"></p>
    <script>
        function fun() {
            let x = document.links.namedItem("link1");
            document.getElementById("links").innerHTML = x;
        }
    </script>
</body>
</html>

支持的浏览器

属性 Chrome Edge Firefox Safari Opera
links 是 1 是 12 是 1 是 1 是 12.1
html_dom_document_reference.htm
广告
© . All rights reserved.