The difference between jquery's Removeprop () and removeattr () removal attributes __removeprop

Source: Internet
Author: User
Tags uuid
The difference between the Removeprop () and removeattr () removal attributes of jquery

------------Removeprop () and removeattr () functions are detailed------------------------------


※removeprop () "Use less delete, set the property value to False"
The Removeprop () method removes the property set by the prop () method.

Note: Removeprop () removes the attribute of the specified name on each element that matches the current jquery object.

Grammar
JQuery 1.6 adds this function. The syntax for the Removeprop () function is as follows:
Jqueryobject.removeprop (PropertyName)
Note: Removeprop () removes the attribute of the specified name on each element that matches the current jquery object.

return value

The return value of the Removeprop () function is the jquery type, returning the current jquery object itself.

some of the built-in properties of the Window object or DOM element are not allowed to be deleted, and if you attempt to remove them, you will cause the browser to produce an error. jquery first assigns the value of this property to undefined and ignores any error messages generated by the browser.


In general, you'd better use the function (that is, Removeprop ()) to remove some of the custom attributes instead of the built-in properties.


Do not use this function (that is, Removeprop ()) to remove the local properties checked, selected, and disabled of the DOM element. This will completely delete the corresponding attribute, and once deleted, you cannot add the corresponding attribute to the DOM element again. Use the prop () function to set it to false, such as: Jqueryobject.prop ("checked", false).

To remove the onclick event:
In IE6 ~ IE8, the removeattr () function cannot remove the OnClick event property in the row, in order to avoid potential problems, use the prop () function with the following code: jqueryobject. prop ("onclick", null );
-----------------------------------------------------------------------------------------------------

Examples and instructions:

Removeprop () Use example:

<div id= "N1" >
      <p id= "n2" class= "demo test" data-key= "UUID" data_value= "1235456465" >codeplayer</p >
</div>
The jquery code is as follows:
//appends a newline label to the current page and the specified HTML content function W (HTML) {$ (document.body). Append ("<br/>" + html);
var $n 2 = $ ("#n2");
$n 2.prop ("prop_a", "Codeplayer");
$n 2.prop ("Prop_b", {name: "Codeplayer", age:20}); W ($n 2.prop ("prop_a")); Codeplayer W ($n 2.prop ("Prop_b"));
[Object]//delete properties on the N2 element object prop_a and Prop_b $n 2.removeProp ("prop_a");
$n 2.removeProp ("Prop_b"); W ($n 2.prop ("prop_a")); Undefined W ($n 2.prop ("Prop_b")); The Undefined//Removeprop () simply deletes the Data-key property of the element (the attribute object) itself//because it does not have a Data-key property of its own, and therefore does not delete any attributes//or element nodes with the above ID n2 (
The Data-key property of the HTML document affects $n 2.removeProp ("Data-key"); W ($n 2.prop ("Data-key")); Undefined//through attr () access the attributes in the element document node W ($n 2.attr ("Data-key")); UUID 

------------------------------------------------------------------------------------------------
The ※removeattr () Definition and Usage removeattr () function removes the attribute specified on each element node that matches the current jquery object. This function belongs to the jquery object (instance). If you need to set or get some attribute values on an element node, you can use the attr () function. Grammar
The syntax for the removeattr () function is as follows:
Jqueryobject.removeattr (Attributenames);
Note : removeattr () removes the attribute of the specified name on each element that matches the current jquery object.
JQuery 1.7 New support: You can pass in a space-delimited string, and each substring separated by a space is the name of the property that needs to be removed.

<div id= "N1" >
    
    
</div>
The jquery code is as follows:
var $imgs = $ ("img");
Removes the Data-id property of all IMG elements
$imgs. removeattr ("Data-id");

var $n 2 = $ ("#n2");
var $n 3 = $ ("#n3");
Document.writeln ($n 2.attr ("Data-id")); Undefined
Document.writeln ($n 3.attr ("Data-id"));//undefined//

starting from jquery 1.7, you can remove both the ALT and Title attributes
// In some versions prior to jquery 1.6, property names containing spaces can throw errors "uncaught invalidcharactererror: 
//The string contains invalid characters. "
///In jquery 1.6.x, the property name contains a space, does not throw an error, but ignores the removal
$imgs. removeattr (" alt title ");
Document.writeln ($n 2.attr ("alt")); Undefined (site name) {parentheses denote//output in jquery 1.6.x
                                      , below} 
Document.writeln ($n 2.attr ("title"));//undefined ( Codeplayer)
Document.writeln ($n 3.attr ("alt"))//undefined (site logo)
Document.writeln ($n 3.attr ("title") ); Undefined (focus on programming development technology sharing)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.