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