1.前言:

前两天写过一片《分析dom元素的特性Attribute和属性Property》,分析了特性和属性的区别。那篇文章却忽略了一个主要知识点——getAttributeNode()和setAttributeNode()

近来看《jQuery技术内幕》,今天正好看到jQuery.attr()那一部分,忽然想起来这个方法。就此简单说一说。

2.从jQuery说起:

jQuery指出,在IE6、7下,浏览器的getAttribute()和setAttribute()不能正常获取和设置Attribute的值。jQuery做的测试类似于:

div1.className = 'aaa';
alert(div1.getAttribute("className") === 'aaa');

IE6、7下或出现以上情况,即通过正常的 getAttribute("class")获取不到值。

那么在这种情况下,jQuery给出的解决方案是通过getAttributeNode("class").nodeValue获取,即可成功。相关代码如下:

 if ( !getSetAttribute ) {

     //省略...

     // Use this for any attribute in IE6/7
// This fixes almost every IE6/7 issue
nodeHook = jQuery.valHooks.button = {
get: function( elem, name ) {
var ret;
ret = elem.getAttributeNode( name );
return ret && ( fixSpecified[ name ] ? ret.nodeValue !== "" : ret.specified ) ?
ret.nodeValue :
undefined;
},
set: function( elem, value, name ) {
// Set the existing or create a new attribute node
var ret = elem.getAttributeNode( name );
if ( !ret ) {
ret = document.createAttribute( name );
elem.setAttributeNode( ret );
}
return ( ret.nodeValue = value + "" );
}
}; //省略...
}

3.如何应用:

3.1 getAttributeNode:

getAttributeNode()用法比较简单,它将返回一个Attr类型的对象,其nodeType === 2

<div id="div1" class="divClass"></div>

var className = div1.getAttributeNode("class").nodeValue;      //'divClass'
var title = div1.getAttributeNode("title"); // null
var type = div1.getAttributeNode("class").nodeType; //

3.2 setAttributeNode:

setAttributeNode()将接收一个Attr类型的对象,Attr类型的对象可用document直接创建:

<div id="div1" class="divClass"></div>

var attr = document.createAttribute("myAttr");
attr.nodeValue = 'wang';
div1.setAttributeNode(attr);

运行以上代码,div1会加上一个“myAttr”的自定义特性:

4.最后:

加上对getAttributeNode()和setAttributeNode()的分析,对于html特性和dom属性的分析就更全面了。

各位看官,如有发现问题,请尽管提出!在此谢过!

js便签笔记(4)——简单说说getAttributeNode()和setAttributeNode()的更多相关文章

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

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

  2. js便签笔记(12)——浏览TOM大叔博客的学习笔记 part2

    1. 前言 昨天写了<js便签笔记(11)——浏览TOM大叔博客的学习笔记 part1>,简单记录了几个问题.part1的重点还是在于最后那个循环创建函数的问题,也就是多个子函数公用一个闭 ...

  3. js便签笔记(13)——jsonp其实很简单【ajax跨域请求】

    前两天被问到ajax跨域如何解决,还真被问住了,光知道有个什么jsonp,迷迷糊糊的没有说上来.抱着有问题必须解决的态度,我看了许多资料,原来如此... 为何一直知道jsonp,但一直迷迷糊糊的不明白 ...

  4. js便签笔记(14)——用nodejs搭建最简单、轻量化的http server

    1. 引言 前端程序猿主要关注的是页面,你可能根本就用不到.net,java,php等后台语言. 但是你制作出来的网页总要运行.总要测试吧?——那就免不了用到http server.我先前都是用vis ...

  5. js便签笔记(11)——浏览TOM大叔博客的学习笔记 part1

    1. 前言 这两天看了一下TOM大叔的<深入理解js系列>中的基础部分,根据自己的实际情况,做了读书笔记,记录了部分容易绊脚的问题.写篇文章,供大家分享. 2. 关于HTMLCollect ...

  6. js便签笔记(10) - 分享:json2.js源码解读笔记

    1. 如何理解“json” 首先应该意识到,json是一种数据转换格式,既然是个“格式”,就是个抽象的东西.它不是js对象,也不是字符串,它只是一种格式,一种规定而已. 这个格式规定了如何将js对象转 ...

  7. js便签笔记(10) - 分享:json.js源码解读笔记

    1. 如何理解“json” 首先应该意识到,json是一种数据转换格式,既然是个“格式”,就是个抽象的东西.它不是js对象,也不是字符串,它只是一种格式,一种规定而已. 这个格式规定了如何将js对象转 ...

  8. js便签笔记(9)——解读jquery源码时记录的一些知识点

    近来一直利用业余时间在看jquery2.1.1源码,大约看了两千行了.平时看的时候,做了一些笔记,贴出来分享. 1. Array.prototype.slice.call 可以将伪数组转化为真正的数组 ...

  9. js便签笔记(3)——切记:appendChild()、insertBefore()是移动element节点!

    appendChild().insertBefore()是移动element节点,看书的时候注意过,也可以做一个简单的例子测试一下: <div id="div1"> & ...

随机推荐

  1. HDU1065 I Think I Need a Houseboat 2016-07-24 13:59 101人阅读 评论(0) 收藏

    I Think I Need a Houseboat Problem Description Fred Mapper is considering purchasing some land in Lo ...

  2. Win7_Ultimate + VS2010 + openGL 配置

    Win7_Ultimate + VS2010 + openGL 配置 0. 前言 OpenGL作为当前主流的图形API之一,它在一些场合具有比DirectX更优越的特性. (1)与C语言紧密结合. O ...

  3. PBOCIC读芯片卡流程

    https://blog.csdn.net/kxd_ysheng/article/details/21178101?_t=t PBOCIC读芯片卡流程,参考上面的博客,整理了一下PBOCIC卡读流程. ...

  4. openwrt,mjpeg流,wifi摄像头与APP联动,拍照、录像

    最近公司好忙,自己主管的产品又忙着上线,好久都没更新博客了. 最近产品在做一款wifi摄像头,摄像头与手机同时连接在一个局域网内,即可实现摄像头图像在手机显示,并且拍照录像等功能 mjpeg是一张一张 ...

  5. node API buffer

    https://cnodejs.org/topic/5189ff4f63e9f8a54207f60c 1.拼接字符串时,String比buffer要快,buffer需要toString().当保存非u ...

  6. Oracle定义DES加密解密及MD5加密函数

    http://blog.csdn.net/xdweleven/article/details/38319351   (1)DES加密函数create or replace functionencryp ...

  7. How To Use XDOLoader to Manage, Download and Upload Files? (文档 ID 469585.1)

    Applies to: BI Publisher (formerly XML Publisher) - Version 5.6.3 to 5.6.3 [Release 5] Information  ...

  8. Send or receive files via Xshell

    1. install lrzsz $ sudo apt-get install lrzsz 2. If you want to send file from your pc to pi, just d ...

  9. Java编程中获取键盘输入实现方法及注意事项

    Java编程中获取键盘输入实现方法及注意事项 1. 键盘输入一个数组 package com.wen201807.sort; import java.util.Scanner; public clas ...

  10. efcore操作mysql,出现System.InvalidOperationException:“No coercion operator is defined between types 'System.Int16' and 'System.Boolean'.”

    这个恶心的问题,只需要把EF的依赖换成 Pomelo.EntityFrameworkCore.MySql 库即可解决