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. Understanding Execution Governors and Limits

    在编写Salesforce后台代码的时候,如果数据量比较大,或者需要与数据库的交互比较频繁的话,那么会抛出一些限制的异常,来提示你让你做进一步的修改. 有这些限制实质上是跟Salesforce是一个云 ...

  2. HDU 5867 Sparse Graph (2016年大连网络赛 I bfs+补图)

    题意:给你n个点m条边形成一个无向图,问你求出给定点在此图的补图上到每个点距离的最小值,每条边距离为1 补图:完全图减去原图 完全图:每两个点都相连的图 其实就是一个有技巧的bfs,我们可以看到虽然点 ...

  3. C#开发微信公众平台-就这么简单(附Demo)(转)

    原文:http://www.cnblogs.com/xishuai/p/3625859.html 写在前面 阅读目录: 服务号和订阅号 URL配置 创建菜单 查询.删除菜单 接受消息 发送消息(图文. ...

  4. Uva 679 Dropping Balls

    这道题如果模拟着来写,思路很简单 #include <iostream> #include <cstring> using namespace std; int T,D,I,c ...

  5. C++11 std::chrono库详解

    所谓的详解只不过是参考www.cplusplus.com的说明整理了一下,因为没发现别人有详细讲解. chrono是一个time library, 源于boost,现在已经是C++标准.话说今年似乎又 ...

  6. js-原型以及继承小案例

    function human(name,tall){ this.name=name; this.tall=tall; this.toSleep=function(){ alert('no sleep' ...

  7. iOS 'The sandbox is not sync with the Podfile.lock错误

    出现以下错误时, diff: /../Podfile.lock: No such file or directory diff: Manifest.lock: No such file or dire ...

  8. iOS系统架构

    1.iOS系统架构 iOS的系统架构分为四个层次 核心操作系统层 (Core OS) 它包括 内存管理 , 文件系统 , 电源管理以及一些其他的操作系统任务, 它可以直接和硬件设备进行交互 核心服务层 ...

  9. 来自于2016.2.24的flag

    今天又做了一套xj模拟题-------打比赛这种事情变得越来越无聊了------既影响自己的计划(虽然看起来很难完成的样子),又扰乱心情.而且题目大都是学习算法之类的,与计划不接轨就非常没有兴趣. 然 ...

  10. Here's what C++ is

    Yes this article describes what c++ exactlyis http://www.skywind.me/blog/archives/1398#comment-3671 ...