Home > Web Front-end > JS Tutorial > body text

How to change the value of href in jquery

青灯夜游
Release: 2021-11-22 18:42:07
Original
5526 people have browsed it

In jquery, you can use the attr() method to change the value of the href attribute. This method can set the attribute value of the selected element. The syntax "$(selector).attr("href"," new url ");" or "$(selector).attr({"href":"new url"})".

How to change the value of href in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

In HTML, the href attribute of the tag is used to specify the URL address.

Because href is an attribute, you can use the attribute operation method attr() to change the value of href in jquery.

attr() method sets or returns the attribute value of the selected element.

Grammar:

$(selector).attr(attribute,value)
$(selector).attr({attribute:value,....})
Copy after login

Example:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
	$("button").click(function(){
		$("a").attr({"href":"https://www.php.cn/"});
		$("link").attr("href","style.css");
	});
});
</script>
</head>
<body>

<a href="#">超链接文本</a><br>
<link rel="stylesheet" type="text/css" href="#" />
<br>
<button>修改href属性</button>

</body>
</html>
Copy after login

How to change the value of href in jquery

##Related video tutorial recommendation:

jQuery tutorial( video)

The above is the detailed content of How to change the value of href in jquery. 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