Properties and properties of superstructure--dom elements (Dojo/dom-prop)

Source: Internet
Author: User

In the previous article explaining Dojo/dom-attr, we know that in some cases, the attr module will be handed to the prop module for processing. Like what:

    • Textcontent, InnerHTML, ClassName, htmlfor, value
    • Disabled, checked, and other stateless attributes correspond to the Boolean variables in the attribute
    • Handling of events

In that section, let's take a look at prop's handling of attributes.

The first is a standard name dictionary, which renames the property names you want to set to avoid conflicts with reserved words:

Exports.names = {        //properties renamed to avoid clashes with reserved words        "class":"ClassName",        " for":"htmlfor",        //Properties written as CamelCaseTabIndex"TabIndex",        ReadOnly:"readOnly", colspan:"ColSpan", frameborder:"frameborder", rowspan:"RowSpan", Textcontent:"textcontent", ValueType:"ValueType"    };

Compared to dom-attr, the Dom-prop module has only two public functions: Prop.get and Prop.set

  Prop.get the function signature of the method is:

Dojo 1.7+ (AMD) require (["dojo/dom-prop"], function (domprop) {  result = Domprop.get ("Mynode", "someattr");});

In addition to the Textcontent property, the other directly takes the value of the square bracket syntax from node: Node[prop]; for textcontent properties, if the element does not support textcontent, It takes the depth-first algorithm to get the nodevalue of all the text nodes under the element:

function GetText (/*Domnode*/node) {        varText ="", ch =Node.childnodes;  for(vari =0N n = ch[i]; i++){            //Skip comments.            if(N.nodetype! =8){                if(N.nodetype = =1) {text+=GetText (n); }Else{text+=N.nodevalue; }            }        }        returntext; }

Because innertext is not a standard attribute, it is not used here; The following is the source of the Get method:

Exports.Get= function Getprop (/*domnode| String*/Node/*String*/name) {Node=Dom.byid (node); //Convert to standard attribute        varLC = Name.tolowercase (), propname = Exports.names[lc] | |name; //handling This particular attribute of Textcontent        if(PropName = ="textcontent"&&!has ("dom-textcontent")){            returngetText (node); }                returnNode[propname];//anything};

The function signature of the Prop.set method is:

Require (["dojo/dom-prop"], function (domprop) {  result = Domprop.set ("Mynode", "someattr", "value");});

In the Attr.set method, many cases are given to prop to deal with, the following we will look at the implementation of the Set method in Prop.

The set method is used to assign values to the attributes of an element, and in practical applications it is necessary to handle the following situations:

    • Parameter decomposition, if you set more than one property at a time, set each property individually
    • If the someattr is a "style", the Dom-style module is processed
    • If SomeAttr is innerHTML, because some elements (tbody, THEAD, TFOOT, TR, TD, Th, caption, Colgroup, col) do not support the innerHTML attribute, you need the curve to salvation, This first clears the child nodes of the element and then uses the Dom-construct Todom method to convert the HTML fragment into a DOM node, which is inserted into the element as a child node
    • If SomeAttr is textcontent, also because some browsers do not support this property, the way to salvation is to clear the child nodes of the element first, and then create the text node as a child node into the element
    • If value is a function, it is considered an add event; it is best not to use the Prop module for event processing or to use the on module to bind events
Exports.Set= function SetProp (/*domnode| String*/Node/*string| Object*/Name/*String?*/value) {Node=Dom.byid (node); varL =arguments.length; //Decomposition Parameters        if(L = =2&&typeofName! ="string"){//inline ' d type check             for(varXinchname) {exports.Set(node, x, Name[x]); }            returnNode//Domnode        }        //If you want to set a style, call Dom-style to handle        varLC = Name.tolowercase (), propname = Exports.names[lc] | |name; if(PropName = ="style"&&typeofValue! ="string"){//inline ' d type check//Special case:setting a styleStyle.Set(node, value); returnNode//Domnode        }        //if it is innerHTML, for the node that does not support innerHTML, adopt the way that the curve salvation, otherwise directly set innerHTML        if(PropName = ="InnerHTML"){            //Special case:assigning HTML//The hash lists elements with read-only InnerHTML on IE            if(Has ("IE") && Node.tagName.toLowerCase ()inch{col:1, Colgroup:1, table:1, Tbody:1, TFOOT:1, Thead:1Tr:1, Title:1}) {Ctr.empty (node);            Node.appendchild (Ctr.todom (value, node.ownerdocument)); }Else{Node[propname]=value; }            returnNode//Domnode        }        //If Textcontent is not supported, the text node is added after the element child node is cleared        if(PropName = ="textcontent"&&!has ("dom-textcontent") {ctr.empty (node);            Node.appendchild (Node.ownerDocument.createTextNode (value)); returnnode; }        //This section binds events through prop, but is not recommended in this way        if(lang.isfunction (value)) {//Special Case:assigning an event handler//clobber If we can            varAttrid =Node[_attrid]; if(!Attrid) {Attrid= _ctr++; Node[_attrid]=Attrid; }            if(!_evthdlrmap[attrid]) {_evthdlrmap[attrid]= {}; }            varh =_evthdlrmap[attrid][propname]; if(h) {//H.remove (); Removes an event if it has been bound in a similar wayconn.disconnect (h); }Else{                Try{delete Node[propname]; }Catch(e) {}}//Ensure that event objects is normalized, etc.            if(value) {//The Prop.get function returns node, so put handle in _evthdlrmap//_evthdlrmap[attrid][propname] = ON (node, propname, value);_evthdlrmap[attrid][propname] =conn.connect (node, propname, value); }Else{Node[propname]=NULL; }            returnNode//Domnode} Node[propname]= value;//assign a value directly to a property        returnNode//Domnode};

  If you think this article is helpful to you, please do not hesitate to click on the bottom right "recommended", Thank you ~

Properties and properties of superstructure--dom elements (Dojo/dom-prop)

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.