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"," ")".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
1. Use the removeAttr method
removeAttr() method to remove one or more elements from the selected element attributes.
$("a").removeAttr("href");
The example is as follows:
123 <script> $(document).ready(function(){ $("button").click(function(){ $("a").removeAttr("href"); }); }); </script> PHP中文网
Output result:
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"," ");
The example is as follows:
123 <script> $(document).ready(function(){ $("button").click(function(){ $("a").attr("href"," "); }); }); </script> PHP中文网
Output result:
After clicking the button and clicking the a label again, the result is:
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!