上一篇返本求源中,我们从DOM基础的角度出发,总结了特性与属性的关系。本文中,我们来看看dojo框架是如何处理特性与属性的。dojo框架中特性的处理位于dojo/dom-attr模块属性的处理为与dojo/dom-prop模块中。

attr.set()

  方法的函数签名为:

require(["dojo/dom-attr"], function(domAttr){
result = domAttr.set("myNode", "someAttr", "value");
});

  “someAttr”代表特性名称,但有时候也可以是一些特殊的属性名,如:‘textContent’:

  

  可以看到上图中使用attr设置innerText只会在html标签中增加innerText这个自定义特性,而无法改变文本,使用textContent却能够达到改变文本的目的。其中缘由就是因为在attr模块建立了forceProps字典,在此字典中的key全部使用prop模块来设置:

        forcePropNames = {
innerHTML: ,
textContent:,
className: ,
htmlFor: has("ie"),
value:
}

  set()方法中主要处理以下几件事:

  • “someAttr”除了可以是字符串外,还可以是key-value对象,所以对于key-value对象我们首先要进行参数分解。
  • 如果someAttr等于style,就交给dojo/dom-style模块来处理
  • 上篇文章中我们说过,特性值只能是字符串,所以对于函数,默认是作为事件绑定到元素上,这部分交给dojo/dom-prop来处理;另外对于disabled、checked等无状态的属性,在通过属性设置时,只能传递布尔值,所以这部分也交给prop来处理
  • 剩下的交给原生api,setAttribute来处理,这个方法会自动调用value的toString方法
exports.set = function setAttr(/*DOMNode|String*/ node, /*String|Object*/ name, /*String?*/ value){
node = dom.byId(node);
if(arguments.length == ){ // 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()

  方法的函数签名为:

// Dojo 1.7+ (AMD)
require(["dojo/dom-attr"], function(domAttr){
result = domAttr.get("myNode", "someAttr");
});

  为了解释方便,我们要先看一下get方法的源码:

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
};
  1. 先得到的是三个变量:propName、forceProp、value,
  2. 如果attrName属于forceProps集合,直接返回DOM节点的属性
  3. textContent明显位于forceProps中,为什么还要单独拿出来做判断?因为有的低版本的浏览器不支持textContent,我们需要利用深度优先算法,利用文本的节点的nodeValue由父到子依次拼接文本,这一点jQuery与dojo的思路都是一致的:
    1. dojo:

      function getText(/*DOMNode*/node){
      var text = "", ch = node.childNodes;
      for(var i = , n; n = ch[i]; i++){
      //Skip comments.
      if(n.nodeType != ){
      if(n.nodeType == ){
      text += getText(n);
      }else{
      text += n.nodeValue;
      }
      }
      }
      return text;
      }
    2. jQuery:
  4. set方法中提到过,对于布尔跟函数,交给prop来设置,那么取值时当然也要从prop中来取;至于为什么要单独拿出href,在“返本求源”中已经说过,通过属性得到的href属性跟getAttribute方法得到的值并不一定相同,尤其是非英文字符:
  5. 由prop模块该做的都做完了,所以这里判断node中是否存在该特性时,无需理会forceProps字典;如果存在则调用getAttribute方法。

attr.has

  既然可以使用attr来set这些属性,那在attr.has方法中,位于此字典中属性当然也要返回true,所以attr.has(node, attrName)方法主要判断两个方面:

  • attrName是否是forceProps中的key
  • attrName是否是一个特性节点。特性节点为与元素的attributes属性中,可以通过:attributes[attrName] && attributes[attrName].specified 来判断
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);
};

  

  如果您觉得这篇文章对您有帮助,请不吝点击右下方“推荐”,谢谢~

上层建筑——DOM元素的特性与属性(dojo/dom-attr)的更多相关文章

  1. 返本求源——DOM元素的特性与属性

    抛砖引玉 很多前端类库(比如dojo与JQuery)在涉及dom操作时都会见到两个模块:attr.prop.某天代码复查时,见到一段为某节点设置文本的代码: attr.set(node, 'inner ...

  2. 上层建筑——DOM元素的特性与属性(dojo/dom-prop)

    上一篇讲解dojo/dom-attr的文章中我们知道在某些情况下,attr模块中会交给prop模块来处理.比如: textContent.innerHTML.className.htmlFor.val ...

  3. js便签笔记(2)——DOM元素的特性(Attribute)和属性(Property)

    1.介绍: 上篇js便签笔记http://www.cnblogs.com/wangfupeng1988/p/3626300.html最后提到了dom元素的Attribute和Property,本文简单 ...

  4. 使用jQuery匹配文档中所有的li元素,返回一个jQuery对象,然后通过数组下标的方式读取jQuery集合中第1个DOM元素,此时返回的是DOM对象,然后调用DOM属性innerHTML,读取该元素 包含的文本信息

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. JavaScript Dom基础-9-Dom查找方法; 设置DOM元素的样式; innerHTML属性的应用; className属性的应用; DOM元素上添加删除获取属性;

    JavaScript Dom基础 学习目标 1.掌握基本的Dom查找方法 domcument.getElementById() Domcument.getElementBy TagName() 2.掌 ...

  6. dom元素上添加断点(使用dom breakpoint找到修改属性的javascript代码)

    使用dom breakpoint能快速找到修改了某一个dom element的JavaScript code位于何处.在Chrome development tool里,选中想要inspect的dom ...

  7. DOM元素的Attribute(特性)和Property(属性) 【转载】

    1.介绍: 上篇js便签笔记http://www.cnblogs.com/wangfupeng1988/p/3626300.html最后提到了dom元素的Attribute和Property,本文简单 ...

  8. jQuery 数据 DOM 元素 核心 属性

    jQuery 参考手册 - 数据 .clearQueue() 从序列中删除仍未运行的所有项目 .clearQueue(queueName) $("div").clearQueue( ...

  9. 获取或操作DOM元素特性的几种方式

    1. 通过元素的属性 可以直接通过元素属性获取或操作特性,但是只有公认的特性(非自定义的特性),例如id.title.style.align.className等,注意,因为在ECMAScript中, ...

随机推荐

  1. bash fifo管道使用测试例子

    碰到一个场景: 一个脚本内起了多个后台线程,往一个日志文件写日志,结果因为线程之间争抢写锁,导致脚本执行效率很低,为了解决这个问题,希望减少写锁的争抢,尝试使用fifo解决该问题,以下是实验用例子. ...

  2. java导入excel时遇到的版本问题

    java中读取excel文件时对不同的版本提供了不同的读取方法,这就要求我们在读取excel文件时获取excel文件的版本信息从而通过不同的版本去使用不同的读取方式, 在WorkbookFactory ...

  3. 3G产品升级相关知识

    1.3G产品升级时,内核,文件系统,boot,应用程序,全都升级了,是在boot里升的; 2.拨号模式里的 chap:公网; pap:专网 3.用来查看U盘挂在哪个目录下: cat /proc/par ...

  4. Redis第一篇(Redis单机版本安装及启动)

    安装: 1 2 3 4 5 [root@M2_Redis1 ~]# yum install gcc gcc-c++     (安装依赖) [root@M2_Redis1 tools]# wget ht ...

  5. 向MySql数据库导入excel表数据

    最近要开发一个小的答题系统,如果题目人工录入那确实很麻烦.所以想到是不是可以从用一些现有数据格式的文件导入数据.在网上查了一下,看到有关于将excel的数据导入到mysql的方法.所以将题库数据整理成 ...

  6. wince中测试驱动应用程序的实现

    这里建的工程是MFC的smart device,选择ARMV4I的指令集,不同的设备可能会有轻微的不同,不过大体实现是一样滴.还有,这里选的应用类型是dialog base. 1.应用监测内核动向 内 ...

  7. uva 11806 Cheerleaders

    // uva 11806 Cheerleaders // // 题目大意: // // 给你n * m的矩形格子,要求放k个相同的石子,使得矩形的第一行 // 第一列,最后一行,最后一列都必须有石子. ...

  8. Centos配置查看

    Reference: [1] http://www.centoscn.com/CentOS/help/2013/0928/1743.html [2] http://www.cnblogs.com/hi ...

  9. 一步一步安装hive

    安装hive 1.下载hive-0.11.0.tar.gz,解压; 2.下载mysql-connector-java-5.1.29-bin.jar并放到hive/lib/下: 3.配置hive/con ...

  10. Android中的requestWindowFeature

    朋友推荐使用博客记录工作中的难点以及常犯的错误,仅作为笔记,首先整理之前的工作日志. requestWindowFeature(featrueId),它的功能是启用窗体的扩展特性: 注意:该方法必须放 ...