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

一:Attribute的几种用法和寄义 getAttribute:获取某一个属性的值; setAttribute:确立一个属性,并同时给属性捆绑一个值; createAttribute:仅确立一个属性; removeAttribute:删除一...

一: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 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>

 

思源资源网:分类流动

1.阿里云: 本站现在使用的是阿里云主机,平安/可靠/稳固。点击领取2000米代金券、领会最新阿里云产物的种种优惠流动点击进入

2.腾讯云: 提供云服务器、云数据库、云存储、视频与CDN、域名等服务。腾讯云各种产物的最新流动,优惠券领取点击进入

3.广告同盟: 整理了现在主流的广告同盟平台,若是你有流量,可以作为参考选择适合你的平台点击进入

链接: http://www.fly63.com/article/detial/4951

  • 发表于 2021-04-03 13:40
  • 阅读 ( 244 )
  • 分类:互联网

0 条评论

请先 登录 后评论
chgvj
chgvj

750 篇文章

你可能感兴趣的文章

相关问题