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. 基于XMPP协议的Android即时通信系

    以前做过一个基于XMPP协议的聊天社交软件,总结了一下.发出来. 设计基于开源的XMPP即时通信协议,采用C/S体系结构,通过GPRS无线网络用TCP协议连接到服务器,以架设开源的Openfn'e服务 ...

  2. android测试点汇总

    Android的功能测试点 安装\卸载 App具体功能点 联网(默认的联网方式是什么?Wifi orSim卡?网络切换是否有相应的提示说明?飞行模式) 程序进入输入功能时,是否正常弹出键盘;键盘是否遮 ...

  3. 伪Acmer的推理(dfs/bfs)

    时间限制:1000MS  内存限制:65535K 提交次数:12 通过次数:9 收入:32 题型: 编程题   语言: C++;C Description 现在正是期末,在复习离散数学的Acmer遇到 ...

  4. Sleep函数的真正用意

    转自:http://blog.csdn.net/boyuejiang/article/details/8908333 关于VOID Sleep(DWORD dwMilliseconds);函数,许多人 ...

  5. Asp.net_完美设置页面最小宽度(兼容ie)

    div+css的布局相比table布局简化了前端开发的复杂性,也会带来一些问题,现在我们就说一下浮动定位在页面大小改变时布局错位的解决办法,给页面设置最小宽度: 只需更改全局css样式表 body { ...

  6. Mac 显示 Finder 隐藏文件

    显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏Mac隐藏文件的命令:defaults writ ...

  7. ps让文字的颜色变成图片的颜色

    第一种: 把某张图片图层置于文字图层之上,右击图片图层-->选择"创建剪贴蒙版"即可.第二种:把某张图片图层置于文字图层之上,并选择图片图层,按住Ctrl键,点击文字图层,载 ...

  8. ACdream1157 Segments(CDQ分治 + 线段树)

    题目这么说的: 进行如下3种类型操作:1)D L R(1 <= L <= R <= 1000000000) 增加一条线段[L,R]2)C i (1-base) 删除第i条增加的线段, ...

  9. The StringFormat property

    As we saw in the previous chapters, the way to manipulate the output of a binding before is shown is ...

  10. 寻找房间中心zz

    Finding the Centroid of a Room Boundary It's been a while since my last post and I'm sure most of yo ...