Source code Analysis of jquery1.7.2 (vi) Basic functions

Source: Internet
Author: User

JQuery.fn.extend ({attr:function (name, value) {return jquery.access (this, jquery.attr, name, value, Arguments.length &G T 1);},removeattr:function (name) {return This.each (function () {jquery.removeattr (this, name);});},prop:function (name , value) {return jquery.access (this, jquery.prop, name, value, Arguments.length > 1);},removeprop:function (name) {name = jquery.propfix[Name] | | Name;return this.each (function () {//Try/catch handles cases where IE balks (such as REM  Oving a property on window) try {this[name] = undefined;delete this[name];} catch (E) {}});},addclass:function (value ) {var classnames, I, L, Elem,setclass, C, Cl;console.log (this) if (jquery.isfunction (value)) {return This.each (functi On (j) {JQuery (this). AddClass (Value.call (This, j, This.classname);});  if (value && typeof value = = = "string") {Classnames = Value.split (Rspace); for (i = 0, L = this.length; I < L i++) {elem = this[i];if (elem.nodetype = = 1) {if (!elem.ClassName && Classnames.length = = 1) {elem.classname = value;} else {setclass = "+ Elem.classname +" "; for ( c = 0, cl = classnames.length; C < cl; C + +) {if (!~setclass.indexof ("" + classnames[C] + ")) {SetClass + = classnames[c] +" ";}} Elem.classname = Jquery.trim (SetClass);}}}  return This;},removeclass:function (value) {var classnames, I, L, Elem, ClassName, C, Cl;if (Jquery.isfunction (value) {return This.each (function (j) {JQuery (this). Removeclass (Value.call (This, j, This.classname);});} if (value && typeof value = = = "string") | | value = = undefined) {classnames = (value | | ""). Split (Rspace); for (i = 0, L = this.length; I < L; i++) {elem = this[i];if (elem.nodetype = = 1 && elem.classname) {if (value) {ClassName = ("" + ELEM.CLASSN Ame + ""). Replace (Rclass, ""); for (c = 0, cl = classnames.length; C < cl; C + +) {ClassName = Classname.replace ("" + classnames[C] + "", "");} Elem.classname = JQuery.Trim (className);} else {elem.classname = "";}}}} return This;},toggleclass:function (value, stateval) {var type = typeof Value,isbool = typeof Stateval = = = "Boolean"; if (Jquery.isfunction (value)) {return This.each (function (i) {jQuery (this). Toggleclass (Value.call (This, I, This.classname, stateval), stateval);}) ;} Return This.each (function () {if (type = = = "string") {//Toggle individual class Namesvar classname,i = 0,self = JQuery ( This), state = Stateval,classnames = Value.split (Rspace) and while ((ClassName = classnames[i++]) {//check each CLASSN Ame given, space seperated liststate = Isbool? State:!self.hasclass (className), self[state? "AddClass": "Removeclass"] (className);} else if (type = = = "Undefined" | | type = = = "Boolean") {if (this.classname) {//Store className if Setjquery._data (this , "__classname__", This.classname);} Toggle whole Classnamethis.classname = This.classname | | Value = = = False? "": Jquery._data (This, "__classname__") | | "";}});},hasclass:function (selector) {var className = "" + selector + "", I = 0,l = This.length;for (; I < L; i++) {if (This[i].nodetype = = = 1 && ("" + This[i].classname + "). Replace (Rclass," "). IndexOf (ClassName) &G T -1) {return true;}} return False;},val:function (value) {var hooks, ret, Isfunction,elem = this[0];if (!arguments.length) {if (elem) {Ho OKs = jquery.valhooks[Elem.type] | | jquery.valhooks[elem.nodeName.toLowerCase ()];if (hooks && "get" in hooks && (ret = Hooks.get (Elem, "va Lue "))!== undefined) {return ret;} ret = Elem.value;return typeof ret = = = = "string"?//Handle most common string Casesret.replace (Rreturn, "")://Handle CAs Es where value is null/undef or Numberret = = null? ": ret;} return;} Isfunction = jquery.isfunction (value); return This.each (function (i) {var = jQuery (This), val;if (this.nodetype! = = 1) {return;} if (isfunction) {val = Value.call (This, I, self.val ());} else {val = value;} Treat NULL/undefined as ""; Convert numbers to stringif (val = = null) {val = "";} else if (typeof val = = = "Number") {val + = "";} else if (Jquery.isarray (val)) {val = Jquery.map (val, function (value) {return value = = null?) "": Value + "";}); Hooks = jquery.valhooks[This.type] | | jquery.valhooks[this.nodeName.toLowerCase ()];//If set returns undefined, fall back to normal settingif (!hooks | |! (" Set "in hooks) | | Hooks.set (This, Val, "value") = = = = undefined) {this.value = val;}});}); Jquery.extend {valhooks: {option: {get:function (Elem) {//Attributes.value is undefined in Blackberry 4.7 but//uses. V Alue. See #6932var val = Elem.attributes.value;return!val | | Val.specified? Elem.value:elem.text;}},select: {get:function (elem) {var value, I, max, option,index = Elem.selectedindex,values = [] , options = Elem.options,one = Elem.type = = = "Select-one";//Nothing is Selectedif (Index < 0) {return null;} Loop through all the selected Optionsi = one? Index:0;max = OnE? Index + 1:options.length;for (; i < Max; i++) {option = options[i];//Don ' t return options that is disabled or I n A disabled optgroupif (option.selected && jQuery.support.optDisabled!option.disabled:option.getattribute ("disabled") = = = null) && (!option.parentnode.disabled | |!jquery.nodename (option.parentnode, "Optgroup"))) {/ /Get The specific value for the optionvalue = jQuery (option). val ();//We don ' t need a array for one selectsif (one) {return value;} Multi-selects return an Arrayvalues.push (value);}} Fixes Bug #2551--select.val () broken in IE after Form.reset () if (one &&!values.length && options.le Ngth) {return jQuery (options[index]). Val (); Return Values;},set:function (Elem, value) {var values = Jquery.makearray (value); JQuery (Elem). Find ("option"). Ction () {this.selected = Jquery.inarray (JQuery (This). val (), values) >= 0;}); if (!values.length) {elem.selectedindex =-1;} Return Values;}}},atTRFN: {val:true,css:true,html:true,text:true,data:true,width:true,height:true,offset:true},attr:function (Elem, n Ame, value, pass) {var ret, hooks, notxml,ntype = elem.nodetype;//don ' t get/set attributes on text, comment and attribut E nodesif (!elem | | nType = = 3 | | nType = = 8 | | nType = = 2) {return;} if (pass && name in JQUERY.ATTRFN) {//equals $ (' div '). HTML (value) return JQuery (elem) [name] (value);} Fallback to prop when attributes is not supported//use Getettribute when $.PROPIF is not supported (typeof Elem.getattribute = = = "Undefi Ned ") {return Jquery.prop (elem, name, value);} When not an XML object notxml = NType!== 1 | | !jquery.isxmldoc (elem);//all attributes be lowercase//Grab necessary hook if one is Definedif (notxml) {name = Name . toLowerCase ();//Assuming that the value passed in is $.attr ("input", "value", 111, 0)//hooks=$.attrhooks[' value ']hooks = jquery.attrhooks[Name] | | (Rboolean.test (name)? boolhook:nodehook);} if (value!== undefined) {if (value = = = null) {jquery.removeattr (elEM, name); return;} else if (hooks && "set" in hooks && notxml && (ret = Hooks.set (elem, value, name))!== undefine d) {return ret;} else {Elem.setattribute (name, "" + value); return value;}} else if (hooks && "get" in hooks && notxml && (ret = Hooks.get (elem, name))!== null) {return RET;}  else {ret = Elem.getattribute (name);//non-existent attributes return null, we normalize to undefinedreturn ret = = = NULL ? undefined:ret;}},removeattr:function (Elem, value) {var propname, attrnames, name, L, isbool,i = 0;if (Value &AMP;&A mp Elem.nodetype = = = 1) {attrnames = Value.tolowercase (). Split (rspace); l = attrnames.length;for (; i < L; i++) {Name = attrnames[I];if (name) {propname = jquery.propfix[name] | | name;isbool = rboolean.test (name);//See #9699 for EX Planation of this approach (setting first, then removal)//Do not do the this for the Boolean attributes (see #10870) if (!isbool {jquery.attr (Elem, Name, "");} Elem. removeattribute (Getsetattribute name:propname);//Set corresponding property to False for Boolean attributesif (is Bool && propname in elem) {elem[propname] = false;}}}}},attrhooks: {type: {set:function (Elem, value) {//We Can ' t allow the type changed (since it causes problems in IE) if (Rtype.test (elem.nodename) && El Em.parentnode) {jquery.error ("Type property can ' t is changed");} else if (!jquery.support.radiovalue && value = = = "Radio" && Jquery.nodename (elem, "input") {//Setting the type on a radio button after the value resets the Value in ie6-9//Reset value to it's default in case type was set after value//this is for element creationvar val = elem. Value;elem.setattribute ("type", value); if (val) {elem.value = val;} Return value;}}},//Use the Value property for back compat//use the Nodehook for button elements in Ie6/7 (#1954) value: { Get:function (Elem, name) {if (Nodehook && jquery.nodename (elem, "button") {return Nodehook.get (elem, name);} Return name in Elem? elem.value:null;},set:function (Elem, value, name) {if (Nodehook && jquery.nodename (elem , "button") {return Nodehook.set (Elem, value, name);} Does not return so this setAttribute is also usedelem.value = Value;}}},propfix: {tabindex: "TabIndex", ReadOnly: "Reado Nly "," for ":" Htmlfor "," Class ":" ClassName ", MaxLength:" MaxLength ", cellspacing:" cellspacing ", cellpadding:" CellPadding ", rowspan:" RowSpan ", colspan:" colspan ", Usemap:" Usemap ", frameborder:" frameborder ", contenteditable:" Contenteditable "},prop:function (elem, name, value) {var ret, hooks, notxml,ntype = elem.nodetype;//don ' t get/set prope Rties on text, comment and attribute nodesif (!elem | | nType = = 3 | | nType = = 8 | | nType = = 2) {return;} Notxml = NType!== 1 | | !jquery.isxmldoc (Elem); if (notxml) {//Fix name and attach hooksname = jquery.propfix[Name] | | name;hooks = JQUERY.P rophooks[name];} if (value!== undefined) {if ( Hooks && "Set" in hooks && (ret = Hooks.set (elem, value, name))!== undefined) {return ret;} else {ret Urn (elem[name] = value);}} else {if (hooks && "get" in hooks && (ret = Hooks.get (elem, name)!== null) {return ret;} else {retur N elem[name];}}},prophooks: {tabIndex: {get:function (Elem) {//Elem.tabindex doesn ' t always return the correct value When it hasn ' t been explicitly set//http://fluidproject.org/blog/2008/01/09/ Getting-setting-and-removing-tabindex-values-with-javascript/var Attributenode = Elem.getAttributeNode ("TabIndex" ); return Attributenode && attributenode.specified parseint (attributenode.value): Rfocusable.test ( Elem.nodename) | | Rclickable.test (elem.nodename) && elem.href 0:undefined;}}}); /ADD the TabIndex Prophook to Attrhooks for Back-compat (different case is intentional) JQuery.attrHooks.tabindex = JQuery . prophooks.tabindex;//Hook for Boolean attributesboolhook = {get:function (Elem,Name) {//Align Boolean attributes with corresponding properties//Fall back to attribute presence where some booleans ar E not Supportedvar attrnode,property = Jquery.prop (elem, name); return property = = = True | | typeof Property!== "Boolean" && (Attrnode = Elem.getattributenode (name)) && Attrnode.nodevalue!== Fals E. Name.tolowercase (): Undefined;},set:function (Elem, value, name) {var propname;if (value = = = False) {//Remove bool EAN attributes when set to Falsejquery.removeattr (Elem, name);} else {//value is true since we know at the it's type Boolean and not false//Set Boolean attributes to the same NA Me and set the DOM propertypropname = jquery.propfix[Name] | | Name;if (propname in Elem) {//Only set the IDL specifically if it already exists on the elementelem[propname] = true; }elem.setattribute (name, Name.tolowercase ());} return name;};

jquery1.7.2 's source code Analysis (vi) basic functions

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.