The JQuery removeattr () method source code interpretation

Source: Internet
Author: User

Removeattr is much simpler than attr's code.

    function (name) {        returnthis. each (function() {            this, name);}    ,

The Jquery.removeattr method is called internally, so we can just look at it.

Removeattr:function(Elem, value) {varname, propname, I= 0,            //core_rnotwhite=/\s+/g            //value exists and value can match non-whitespace characters            //The nice thing about this step is that it quietly turns a multi-space-delimited string into an array, so removeattr can remove multiple attributes at the same time.Attrnames = value &&Value.match (Core_rnotwhite); if(attrnames && Elem.nodetype = = = 1) {//attribute Node             while(name = attrnames[i++]) ) {                //Propfix Property Correctionpropname = jquery.propfix[Name] | |name; //Boolean attributes get special treatment (#10870)                //jquery.expr.match.bool=/^ (?: Checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap |loop|multiple|open|readonly|required|scoped) $/i                if(jQuery.expr.match.bool.test (name)) {//Set corresponding property to Falseelem[PropName] =false;            } elem.removeattribute (name); }        }    }
Console a little jquery.propfix, we found that it turned out to be such an object:

Cellpadding-->cellpadding ...

is the correction of the case, all the way to the small hump

Class-->classname for-->htmlfor

is to consider the keyword in JS, so the class map to ClassName, JS can be used in the native Element.getattribute ("ClassName")

Look at JQuery.expr.match.bool this regular, it matches these attributes such as checked, selected, async are bool attributes, jquery Why to add a special sentence
elem[propname] = false;

Because for the low version of IE (6, 7), removeattribute alone cannot remove the bool attribute. Without this sentence, we will return "checked" when we $ (). attr ("checked").

The test is as follows:
DEMO1
<body><input id= "ck" type= "checkbox" Checked><script type= "Text/javascript" >var ck= document.getElementById (' ck '); // Ck.checked=false; Ck.removeattribute (' checked '); alert (Ck.getattribute ("checked")); </scrip

DEMO2

<body><input id= "ck" type= "checkbox" Checked><script type= "Text/javascript" >var ck= document.getElementById (' ck '); ck.checked=false; Ck.removeattribute (' checked '); Alert (Ck.getattribute ("checked")); </script></body>



The JQuery removeattr () method source code interpretation

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.