js中的attributes和Attribute的用法和区别。

一:Attribute的几种用法和含义

getAttribute:获取某一个属性的值;

setAttribute:建立一个属性,并同时给属性捆绑一个值;

createAttribute:仅建立一个属性;

removeAttribute:删除一个属性;

getAttributeNode:获取一个节点作为对象;

setAttributeNode:建立一个节点;

removeAttributeNode:删除一个节点;

1.getAttribute:

<body>
    <div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
</body>
<script>
    var d=document.getElementById("sss").getAttribute("value");
    console.log(d)            //aaa;
</script>

get 取得的返回值是属性值。

2.setAtribute:

<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
</body>
<script>
     var d = document.createAttribute("good");
     document.getElementById("sss").setAttributeNode(d);
     alert(document.getElementById("t").innerHTML)   //弹出框<input type="hidden" id="sss" value="aaa" good="">;                                //多了一个属性为good;但是没有给其设置属性值;所以为空。
</script>
// obox.setAttribute("a","b")  返回值是undifined;表示给标签里面加上一个属性a;属性值
                                   // 为b;若设置的属性已经存在,那么仅限设置/更改值;直接设置
                                    //给了标签,看不到返回值,但在html页面中可以看到属性已经被添加到了标签中。

3.createAttribute:

<body>
<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
</body>
<script>
     var d = document.createAttribute("good");
     document.getElementById("sss").setAttributeNode(d);
     alert(document.getElementById("t").innerHTML)  //弹出框<input type="hidden" id="sss" value="aaa" good="">;
                                                     //多了一个属性,属性值为空
</script>

4.removeAttribute:

<body>
<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
</body>
<script>
     var d = document.getElementById("sss").getAttributeNode("value")
     document.getElementById("sss").removeAttributeNode(d);
     alert(document.getElementById("t").innerHTML);   //弹出框<input type = "hidden" id = "sss">;
                                                       //在标签中删除属性value
</script>

getAttribute,setAttribute,createAttribute,removeAttribute四兄弟的概念比较容易理解
,使用方法也比较简单,唯一需要注意这几点:
1、createAttribute在使用的时候不需要基于对象的,document.createAttribute()就可以。
2、setAttribute,createAttribute在使用的时候如果是使用的时候不要使用name,type,value等单词.
3、createAttribute在使用的时候如果只定义了名字,没有d.nodeValue = "hello";语句定义值,FF会认为是一个空字符串,IE认为是undefined。

getAttributeNode,setAttributeNode,removeAttributeNode三个方法的特点是都直接操作一个node(节点)。

例:

<body>
<div id = "t"><input type = "hidden" id = "sss" value = "aaa"></div>
</body>
<script>
     var d = document.createAttribute("good");
     document.getElementById("sss").setAttributeNode(d);
     alert(document.getElementById("t").innerHTML);   //弹出框<input type="hidden" id="sss" value="aaa" good="">;
</script>

setAttributeNode() 方法用于添加新的属性节点。参数:attributenode;必须填写你要添加的属性节点。

如果元素中已经存在指定名称的属性,那么该属性将被新属性替代。如果新属性替代了已有的属性,则返回被替代的属性,否则返回 NULL。

======================================================================

二:attributes的用法:

  attributes可以获取一个对象中的一个属性,并且作为对象来调用,注意在这里要使用“[]”;attributes 属性返回指定节点属性的集合。你可以使用 length 属性确定属性的数量,然后你可以遍历所有的属性节点提取你想要的信息。 每个属性都是可用属性节点对象。

  节点的方法,前缀一定是节点。

  对象.attributes                //获得所有属性节点,返回一个数组(伪数组)

<body>
    <div id = "t">
    <input type = "text" id = "sss" value = "aaa">
</body>
<script type="text/javascript">
    var a=document.getElementById("sss").attributes;
    console.log(a);    //NamedNodeMap {0: type, 1: id, 2: value, type: type, id: id, value: value, length: 3};                    //attributes获得所有的属性节点,返回一个数组(伪数组);

    // attributes可以获取一个对象中的一个属性,并且作为对象来调用,注意在这里要使用“[]”
    var d = document.getElementById("sss").attributes["value"];
    console.log(typeof d);              // object
    console.log(d);                        // value="aaa";
     document.write(d.name);             //显示 value
     document.write(d.value);            //显示 aaa
</script>
<body>
    <div class="box" a="10" b="20" id="cont"></div>
</body>
<script>
    var obox=document.querySelector(".box");
    console.log(obox.attributes[3])           //id="cont";

    console.log(typeof obox.attributes[3])      //object;

    console.log(obox.attributes[3].nodeName);    //id;显示数组中第四个属性的属性名

    console.log(obox.attributes[3].nodeValue);   //cont;显示数组中第四个属性的属性值

    console.log(obox.attributes[3].nodeType);    //2;  元素节点属性的返回值为2

</script>

原文地址:https://www.cnblogs.com/zhouqingfeng/p/11406240.html

时间: 2024-12-12 05:38:39

js中的attributes和Attribute的用法和区别。的相关文章

浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂

var num = 1;    var str = '1';    var test = 1;    test == num  //true 相同类型 相同值    test === num //true 相同类型 相同值    test !== num //false test与num类型相同,其值也相同, 非运算肯定是false    num == str  //true 把str转换为数字,检查其是否相等.    num != str  //false == 的 非运算    num ==

js中的find(),filter(),has()的用法和区别

filter():操作当前元素集,删除不匹配的元素,得到一个新的集合 <ul> <li class="a">list item 1</li> <li>list item 2 <ul> <li><div><span>a</span></div>list item 2-a</li> <li>list item 2-b</li> <

js中return;、return true、return false;区别

一.返回控制与函数结果 语法为:return 表达式 语句结束函数执行,返回调用函数,而且把表达式的值作为函数的结果 二.返回控制 无函数结果,语法为:return; 在大多数情况下,为事件处理函数返回false,可以防止默认的事件行为.例如,默认情况下点击一个<a>元素,页面会跳转到该元素href属性指定的页面. return false; 就相当于终止符, return true;  就想当于执行符. 在js中,return false的作用一般是用来取消默认动作的.比如你单击一个链接除了

JS中undefined、null以及NaN之间的区别,以及对象属性赋值的面试题

(1)以下三种情况typeof 返回类型为undefined --当变量未初始化时 --变量未定义时 --函数无明确返回值时(函数没有返回值时返回的都是undefined) (2)Null 类型 undefined 是由null派生处理的,因此undefined == null undefined 是声明了但是没有初始化的该变量, null表示尚未存在的对象 . (3)NaN 值 是一个特殊值,表示非数(Not a Number),类型转换失败就会返回NaN --NaN 不等于自己,即 NaN

关于JS中数组splice,concat的用法和AngularJs中filter的补充知识

一 JS数组相关操作 1. splice函数,可做插入,删除,替换操作 1 <script> 2 "use strict" 3 var arr = ['z3']; 4 arr.splice(1,0,'l4'); 5 alert(arr); //z3,l4 6 arr.splice(1,1,'w5'); 7 alert(arr); //z3,w5 8 arr.splice(1,1); 9 alert(arr); //z3 10 arr.splice(10,1,'l4','w5

js中String对象slice()方法跟subString()的区别

slice() 和 substring() ECMAScript 提供了两种方法从子串创建字符串值,即 slice() 和 substring().这两种方法返回的都是要处理的字符串的子串,都接受一个或两个参数.第一个参数是要获取的子串的起始位置,第二个参数(如果使用的话)是要获取子串终止前的位置(也就是说,获取终止位置处的字符不包括在返回的值内).如果省略第二个参数,终止位就默认为字符串的长度. 与 concat() 方法一样,slice() 和 substring() 方法都不改变 Stri

JS 中document.URL 和 window.location.href 的区别

实际上,document 和 window 这两个对象的区别已经包含了这个问题的答案. document 表示的是一个文档对象,window 表示一个窗口对象. 一个窗口下面可以有很多的document对象.每个document 都有 一个URL. 但是,这不是所有的区别.当你ctrl + F5 一个链接 http://yourhost.com/#fragment 打印 alert(document.URL ); 和 alert(window.location.href); 发现,这两个的值不一

JS中substring,substr和splice的用法与区别

一.定义和用法 1.substring substring() 方法用于提取字符串中介于两个指定下标之间的字符. substring() 方法返回的子串包括 开始 处的字符,但不包括 结束 处的字符. 语法:string.substring(from, to) 参数 描述 from 必需.一个非负的整数,规定要提取的子串的第一个字符在 string Object 中的位置.(负参数都直接转换为0) to 可选.一个非负的整数,比要提取的子串的最后一个字符在 string Object 中的位置多

JS中document对象和window对象有什么区别

简单来说,document是window的一个对象属性.Window 对象表示浏览器中打开的窗口.如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框架创建一个额外的 window 对象.所有的全局函数和对象都属于Window 对象的属性和方法.document 对 Document 对象的只读引用.