getAttribute和setAttribute只能用于元素节点。

1.当用getElementById获得元素节点时

/*---------------------------index.html---------------------------*/

<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Author" content="Hehe">
  <title>Shopping list</title>
</head>
  <body>
   <p id="purchases" title="one">What to buy</p>
   <script type="text/javascript" src="script.js"></script>
  </body>
</html>

/*------------------------script.js---------------------------*/

var paras=document.getElementById("purchases");
alert(paras.getAttribute("title"));
paras.setAttribute("title","abcd");
alert(paras.getAttribute("title"));

此时警告框一个显示one,一个显示abcd。

2.当用getElementsByTagName获得元素节点时

/*---------------------index.html-----------------------------*/

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Author" content="Hehe">
  <title>Shopping list</title>
</head>
  <body>
   <p id="purchases" title="one">What to buy</p>
    <p title="two">What to buy</p>
   <script type="text/javascript" src="script.js"></script>
  </body>
</html>

/*----------------------------script.js--------------------*/

var paras=document.getElementsByTagName("p");
for(var i=0;i<paras.length;i++){
     paras[i].setAttribute("title","abcd");
     alert(paras[i].getAttribute("title"));
}

此时有俩个警告框显示abcd。

:getElementById返回的是节点,getElementsByTagName返回的是数组。

getAttribute("")与setAttribute("",A)都需要用到双引号A="字符"(即1.setAttribute("","a list of and so on")。2.A=”a list of and so on";setAttribute("",A)。)。

alert()不是显示字符串不需要用到双引号。

getAttribute与setAttribute用法的更多相关文章

  1. 关于getAttribute()和setAttribute()的总结

    继续声明:欲练其功,必先自宫.博主正处在自宫阶段,修炼得道者多多指教. 最近在看<JavaScript DOM 编程艺术>这本书,看到了getAttribute()和setAttribut ...

  2. 请求转发、包含、重定向 getAttribute 和 setAttribute POST和GET编码

     一.请求转发  请求包含  请求重定向 Demo5.java   注意:doPost()方法中别忘写doGet(request, response); public void doGet(HttpS ...

  3. getAttribute、setAttribute、removeAttribute

    1.函数语法 elementNode.attributes:属性返回包含被选节点属性的 NamedNodeMap. elementNode.getAttribute(name):方法通过名称获取属性的 ...

  4. request.setAttribute()用法

    小问题: JSP1代码 String [] test=new String[2]; test[0]="1"; test[1]="2"; request.setA ...

  5. Javascript进阶篇——(DOM—getAttribute()、setAttribute()方法)—笔记整理

    getAttribute()方法通过元素节点的属性名称获取属性的值.语法: elementNode.getAttribute(name) 1. elementNode:使用getElementById ...

  6. JavaScript中的setAttribute用法

    我们经常需要在JavaScript中给Element动态添加各种属性,这可以通过使用setAttribute()来实现,这就涉及到了浏览器的兼容性问题. setAttribute(string nam ...

  7. $.prop()和$.attr() 区别用法

    都用于读取和设置DOM元素节点的属性 不同: $.attr()用于DOM元素本身的属性 $.prop()用于DOM节点对应的JS属性(源于DOM元素到JS对象的映射) 源于两者在jquery类库的实现 ...

  8. jQuery中attr()、prop()、data()用法及区别

    .attr(),此方法从jq1.0开始一直存在,官方文档写的作用是读/写DOM的attribute值,其实1.6之前有时候是attribute,有时候又是property..prop(),此方法jq1 ...

  9. jQuery attr() prop() data()用法及区别

    .attr(),此方法从jq1.0开始一直存在,官方文档写的作用是读/写DOM的attribute值,其实1.6之前有时候是attribute,有时候又是property..prop(),此方法jq1 ...

随机推荐

  1. Redis在Windows下的安装和使用

    NoSQL简介 介绍redis前,我想还是先认识下NoSQL,即not only sql, 是一种非关系型的数据存储,key/value键值对存储.现有Nosql DB 产品: Redis/Mongo ...

  2. An easy problem

    An easy problem Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  3. SpringMyBatis解析4-MapperScannerConfigurer

    如果有成百上千个dao接口呢,那我们岂不是要配置添加成百上千个bean,当然不是这样,spring还为MyBatis添加了拓展的功能,可以通过扫描包目录的方式,添加dao,让我看看具体使用和实现. & ...

  4. Uva 129 Krypton Factor

    0.这道题的输出 处理起来挺麻烦的 以后类似的可以借鉴一下 ;i<cur;i++) { && i%==) printf("\n%c",a[i]); & ...

  5. IconFont和FontAwesome的区别?

    一.[Iconfont] Iconfont支持所有低版本浏览器: Iconfont的图标库更大: Iconfont可以用自己上传的svg,但是要花费大量时间和耐心去设计AI图标: Iconfont的使 ...

  6. wpf Popup Win8.0 bug HorizontalOffset 弹出位置偏移

    问题描述参考 wpf 客户端[JDAgent桌面助手]开发详解(四) popup控件的win8.0的bug 当开发完程序后,我们在多操作系统测试时候发现:win8.0  系统中 popup 弹出的位置 ...

  7. jquerymobile 基础教程

    http://www.w3cplus.com/blog/tags/331.html?page=1

  8. 后缀数组 POJ 2406 Power Strings

    题目链接 题意:连续重复子串.给定一个字符串 L,已知这个字符串是由某个字符串 S 重复 R 次而得到的(L = S^R ), 求 R 的最大值. 分析:枚举长度,判断条件是能被总长度整除且LCP ( ...

  9. js公有、私有、静态属性和方法的区别

          现下,javascript大行其道,对于网站开发人员来说,javascript是必需掌据的一门语言,但随着jquery等框架的流行和使用,许多人对于原生javascript缺乏深入的理解, ...

  10. java基础-控制流语句

    浏览以下内容前,请点击并阅读 声明 一般情况下,代码的执行按照从上到下的顺序,然而通过加入一些判断,循环和跳转语句,你可以有条件地执行特定的语句. 接下来分三部分介绍Java的控制流语句,他们是判断语 ...