Home > Article > Web Front-end > How to remove the attribute of a tag in jquery
Two methods: 1. Directly use removeAttr() to remove the specified attribute of the a tag, the syntax is "$("a").removeAttr("The name of the attribute to be removed")". 2. Use attr() to set the value of the attribute specified by the a tag to empty, using the syntax "$("a").attr("The name of the attribute to be removed","")".
The operating environment of this tutorial: windows7 system, jquery3.2.1 version, Dell G3 computer.
Two methods for jquery to remove attributes specified by a tag
1. Use removeAttr()
removeAttr() method removes the specified attribute from the selected element. Syntax:
$("a").removeAttr("要移除的属性名")
Example: Remove the target attribute of the a tag
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="js/jquery-3.2.1.min.js"></script> <script> $(document).ready(function() { $("button").bind("click", function() { $("a").removeAttr("target"); }); }); </script> </head> <body> <a href="#" target="_blank">a标签超链接</a><br><br> <button>去除a标签的属性</button> </body> </html>
2. Use attr()
attr() method sets or returns the attribute value of the selected element.
When the attr() method is used to set the attribute value of the selected element to null, the specified attribute can also be invalidated.
Syntax:
$("a").attr("要移除的属性名","")
Example: Remove the href attribute of the a tag
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="js/jquery-3.2.1.min.js"></script> <script> $(document).ready(function() { $("button").bind("click", function() { $("a").attr("href", ""); }); }); </script> </head> <body> <a href="#" target="_blank">a标签超链接</a><br><br> <button>去除a标签的属性</button> </body> </html>
Extended knowledge:
element is the href attribute, which specifies the target of the link.
Attributes that can be set by tags
Attributes | Value | Description |
---|---|---|
charset | char_encoding | Not supported in HTML 5. |
coords | coordinates | Not supported in HTML 5. |
href | URL | The target URL of the link. |
hreflang | language_code | Specifies the base language of the target URL. Only used if the href attribute is present. |
media | media query |
Specifies the media type of the target URL. Default value: all. Only used if the href attribute is present. |
name | section_name | Not supported in HTML 5. |
|
Requires current The relationship between the document and the target URL. Only used when href attribute exists. | |
text | ##Not supported in HTML 5.##shape | |
default |
|
##target |
_blank | _parent
|
type |
Specifies the MIME type of the target URL. Only used if the href attribute is present. | Note: MIME = Multipurpose Internet Mail Extensions.[Recommended learning: |
The above is the detailed content of How to remove the attribute of a tag in jquery. For more information, please follow other related articles on the PHP Chinese website!