Home > Web Front-end > Front-end Q&A > How to invalidate the href of a element in jquery

How to invalidate the href of a element in jquery

WBOY
Release: 2022-05-30 11:00:35
Original
2635 people have browsed it

Method: 1. Use the removeAttr method to remove the href attribute of the a element. The syntax is "$("a").removeAttr("href")"; 2. Use the attr method to remove the href attribute of the a element. The attribute is set to empty and the syntax is "$("a").attr("href"," ")".

How to invalidate the href of a element in jquery

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

How jquery invalidates the href of an element

1. Use the removeAttr method

removeAttr() method to remove one or more elements from the selected element attributes.

$("a").removeAttr("href");
Copy after login

The example is as follows:





123

<script>
$(document).ready(function(){
$("button").click(function(){
$(&quot;a&quot;).removeAttr(&quot;href&quot;);
});
});
</script>


PHP中文网


Copy after login

Output result:

How to invalidate the href of a element in jquery

2. Use attr method

attr() method sets or returns the attributes and values ​​of the selected element.

When this method is used to return an attribute value, the value of the first matching element is returned.

When this method is used to set attribute values, one or more attribute/value pairs are set for the matching element.

$("a").attr("href"," ");
Copy after login

The example is as follows:





123

<script>
$(document).ready(function(){
$("button").click(function(){
$(&quot;a&quot;).attr(&quot;href&quot;,&quot; &quot;);
});
});
</script>


PHP中文网


Copy after login

Output result:

How to invalidate the href of a element in jquery

After clicking the button and clicking the a label again, the result is:

How to invalidate the href of a element in jquery

Recommended related video tutorials: jQuery video tutorial

The above is the detailed content of How to invalidate the href of a element 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