如何在 JavaScript 中查找文档中链接的 href 属性?
在本文中,我们将学习如何在 JavaScript 中查找文档中链接的 href 属性。
JavaScript 中的 DOM 属性提供了许多属性,例如 <head>、<title>、<body>、<link>、<a>、<style>。我们可以用两种方法在 HTML DOM 中表示链接。DOM 对象提供 links 属性。
锚标记中的 href 属性指定 URL。要获取 href 属性,我们需要找到锚对象的引用。
为了更好地理解这个概念,让我们进一步了解本文中锚对象的 href 属性的语法和用法。
语法
获取锚对象 href 属性的语法如下所示。
anchorObject.href
示例 1
以下是一个示例程序,使用 getElementsByTagName() 和 getElementByID() 方法获取锚对象的 href 属性。
<!DOCTYPE html>
<html>
<head>
<title>To find the href attribute of a link in a document in JavaScript</title>
</head>
<body style="text-align : center">
<h3>To find the href attribute of a link in a document in JavaScript</h3>
<a name="Google" href="https://www.google.com/" title="Home page for Google">Google</a><br>
<a name="Facebook" href="https://#/" title="Home Page for Facebook">Facebook</a><br>
<a name="Youtube" href="https://www.youtube.com/" title="Home Page for Youtube">Youtube</a><br>
<p id="text1"></p>
<script>
document.getElementById("text1").innerHTML = "The link for first anchor element is : "+document.getElementsByTagName("a")[0].href+"<br/>"+" The link for the id 'yo' is : "+document.getElementById('yo').href;
</script>
</body>
</html>
执行上述代码后,将生成以下输出。
示例 2
以下是一个示例程序,使用 querySelector() 方法获取锚对象的 href 属性。
<!DOCTYPE html>
<html>
<head>
<title>To find the href attribute of a link in a document in JavaScript</title>
</head>
<body style="text-align : center">
<h3>To find the href attribute of a link in a document in JavaScript</h3>
<a name="Google" href="https://www.google.com/" title="Home page for Google">Google</a><br>
<a name="Facebook" href="https://#/" title="Home Page for Facebook">Facebook</a><br>
<a name="Youtube" href="https://www.youtube.com/" title="Home Page for Youtube">Youtube</a><br>
<p id="text1"></p>
<script>
// The document.querySelector('a') will return the first element within the document that matches the specified selector.
document.getElementById("text1").innerHTML = "The link for the first anchor element is : "+document.querySelector('a').href;
</script>
</body>
</html>
执行上述代码后,将生成以下输出。
示例 3
以下是一个示例程序,获取文档内所有 href 标记链接。
<!DOCTYPE html>
<html>
<head>
<title>To get all the href links in a document in JavaScript</title>
</head>
<body style="text-align : center">
<h3>To display all the href links in a document in JavaScript</h3>
<a name="google" href="https://www.google.com/" title="Google Home Page">Google</a><br>
<a name="Facebook" href="https://#/" title="Home Page for Facebook">Facebook</a><br>
<a name="youtube" href="https://www.youtube.com/" title="Youtube Home Page">Youtube</a><br>
<p id="text1"></p>
<script>
var result="";
for(var i=0;i<document.links.length;i++){
result += "Title "+(i+1)+" : "+document.getElementsByTagName('a')[i].title+"<br/>"+" Link "+(i+1)+" : "+document.links[i].href+"<br/>";
}
document.getElementById("text1").innerHTML = "The links in the document are : "+'<br/>'+result;
</script>
</body>
</html>
执行上述代码后,将生成以下输出。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP