Can jquery get the content of a link?

青灯夜游
Release: 2022-09-09 18:34:07
Original
2043 people have browsed it

can be obtained. There are two ways to get the link content: 1. Use jquery text() to get the link text content, the syntax "$("a").text()"; 2. Use jquery attr() to get the link url content, that is, the href attribute The value is sufficient, the syntax is "$("a").attr("href")".

Can jquery get the content of a link?

The operating environment of this tutorial: windows7 system, jquery3.6.1 version, Dell G3 computer.

jquery can get the content of the link.

In a web page, use the tag to set a hypertext link.

<a href="url">链接文本</a>
Copy after login

So the link content can be divided into two situations:

  • Link text content

  • Link url content

Jquery has different acquisition methods for different situations.

1. Use jquery text() to obtain the link text content.

text() method can return the text content of the selected element and will return the combination of all matching elements. Text content (HTML tags will be removed).

$("a").text();
Copy after login

Example:



	
		
		
		<script>
			$(document).ready(function() {
				$("button").click(function() {
					var con=$(&quot;a&quot;).text();
					console.log(con);
				});
			});
		</script>
	
	
		超链接文本

Copy after login

Can jquery get the content of a link?

2. Use jquery attr() to get the link url content

The content of the link url is the value of the href attribute; and attr() can return the attribute value of the selected element.

$(selector).attr(attribute)
Copy after login
  • attribute: Specifies the attribute whose value is to be obtained.

Just use attr() to return the value of the href attribute of the a element.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<script src="js/jquery-3.6.0.min.js"></script>
		<script>
			$(document).ready(function() {
				$("button").click(function() {
					var con=$("a").attr("href");
					console.log(con);
				});
			});
		</script>
	</head>
	<body>
		<a href="https://www.php.cn/">超链接文本</a><br><br>
		<button>链接url内容</button>
	</body>
</html>
Copy after login

Can jquery get the content of a link?

[Recommended learning: jQuery video tutorial, web front-end video

The above is the detailed content of Can jquery get the content of a link?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template