上一篇返本求源中,咱们从DOM基础的角度出发,总结了特性与属性的关系。本文中,咱们来看看dojo框架是如何处理特性与属性的。dojo框架中特性的处理位于dojo/dom-attr模块属性的处理为与dojo/dom-prop模块中。html
attr.set()node
方法的函数签名为:算法
require(["dojo/dom-attr"], function(domAttr){ result = domAttr.set("myNode", "someAttr", "value"); });
“someAttr”表明特性名称,但有时候也能够是一些特殊的属性名,如:‘textContent’:api
能够看到上图中使用attr设置innerText只会在html标签中增长innerText这个自定义特性,而没法改变文本,使用textContent却可以达到改变文本的目的。其中原因就是由于在attr模块创建了forceProps字典,在此字典中的key所有使用prop模块来设置:浏览器
forcePropNames = { innerHTML: 1, textContent:1, className: 1, htmlFor: has("ie"), value: 1 }
set()方法中主要处理如下几件事:框架
exports.set = function setAttr(/*DOMNode|String*/ node, /*String|Object*/ name, /*String?*/ value){ node = dom.byId(node); if(arguments.length == 2){ // inline'd type check // the object form of setter: the 2nd argument is a dictionary for(var x in name){ exports.set(node, x, name[x]); } return node; // DomNode } var lc = name.toLowerCase(), propName = prop.names[lc] || name, forceProp = forcePropNames[propName]; if(propName == "style" && typeof value != "string"){ // inline'd type check // special case: setting a style style.set(node, value); return node; // DomNode } if(forceProp || typeof value == "boolean" || lang.isFunction(value)){ return prop.set(node, name, value); } // node's attribute node.setAttribute(attrNames[lc] || name, value); return node; // DomNode };
attr.get()dom
方法的函数签名为:函数
// Dojo 1.7+ (AMD) require(["dojo/dom-attr"], function(domAttr){ result = domAttr.get("myNode", "someAttr"); });
为了解释方便,咱们要先看一下get方法的源码:ui
exports.get = function getAttr(/*DOMNode|String*/ node, /*String*/ name){ node = dom.byId(node); var lc = name.toLowerCase(), propName = prop.names[lc] || name, forceProp = forcePropNames[propName], value = node[propName]; // should we access this attribute via a property or via getAttribute()? if(forceProp && typeof value != "undefined"){ // node's property return value; // Anything } if(propName == "textContent"){ return prop.get(node, propName); } if(propName != "href" && (typeof value == "boolean" || lang.isFunction(value))){ // node's property return value; // Anything } // node's attribute // we need _hasAttr() here to guard against IE returning a default value var attrName = attrNames[lc] || name; return _hasAttr(node, attrName) ? node.getAttribute(attrName) : null; // Anything };
function getText(/*DOMNode*/node){ var text = "", ch = node.childNodes; for(var i = 0, n; n = ch[i]; i++){ //Skip comments. if(n.nodeType != 8){ if(n.nodeType == 1){ text += getText(n); }else{ text += n.nodeValue; } } } return text; }
attr.hasthis
既然能够使用attr来set这些属性,那在attr.has方法中,位于此字典中属性固然也要返回true,因此attr.has(node, attrName)方法主要判断两个方面:
exports.has = function hasAttr(/*DOMNode|String*/ node, /*String*/ name){ var lc = name.toLowerCase(); return forcePropNames[prop.names[lc] || name] || _hasAttr(dom.byId(node), attrNames[lc] || name); // Boolean }; function _hasAttr(node, name){ var attr = node.getAttributeNode && node.getAttributeNode(name); return !!attr && attr.specified; // Boolean }
attr.remove
这个方法比较简单,直接调用了removeAttribute方法
exports.remove = function removeAttr(/*DOMNode|String*/ node, /*String*/ name){ // summary: // Removes an attribute from an HTML element. // node: DOMNode|String // id or reference to the element to remove the attribute from // name: String // the name of the attribute to remove dom.byId(node).removeAttribute(attrNames[name.toLowerCase()] || name); };
若是您以为这篇文章对您有帮助,请不吝点击右下方“推荐”,谢谢~