setAttribute和getAttribute
2014-04-12 22:52
387 查看
自己在写js时用到了这两个方法。但碰到了问题,百度不给力,只能自己上了。用法:obj.setAttribute(attr,value);obj.getAttribute(attr)。obj是元素节点,attr和value是传入的参数,这个大家都知道。下面我说个大家不知道的。<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title> 属性操作 </title></head><body><ul id="aa"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li></ul><button id="btn">click</button><script type="text/javascript">function attr(obj,attri,ovalue){switch(arguments.length){case 2:return obj.getAttribute(attri);break;case 3:obj.setAttribute(attri,ovalue);break;default:break;}}var oul = document.getElementById("aa");var olis = oul.getElementsByTagName('li');var obtn = document.getElementById("btn");oul.index = 1;obtn.onclick = function(){alert(attr(oul,'index'))};</script></body></html> 你把这段代码运行一下,单击按钮,正确的弹出了 1 了吗?各个浏览器中的效果:IE:文档模式9标准IE:文档模式8标准不是所有的浏览器中一定有问题,但我们追求的是一点问题都没有。继续看下去:将倒数第五行的:oul.index = 1;换成attr(oul,'index',1); 再到浏览器去看一遍。就不截图了,都是 1 嘛。
相关文章推荐
- DOM方法(getElementById, getElementsByTagName, getAttribute, setAttribute)
- 设置 取消input的disabled属性 getattribute(),setAttribute(),removeattribute()用法
- getAttribute:取得属性; setAttribute:设置属性。
- dom(getElementsByName()\getElementsByName()\getAttribute()\setAttribute()\节点属性)
- getAttribute、setAttribute 、 removeAttribute
- DOM自定义属性 getAttribute、setAttribute、removeAttribute
- getAttribute 和setAttribute 的使用方法
- getAttribute与setAttribute
- setAttribute()、getAttribute()与ele[attr]与自定义属性
- getAttribute()方法和setAttribute()
- 利用JS脚本通过getAttribute()和setAttribute()等对CSS样式进行操作
- InvocationTargetException异常的深入研究-servlet的setAttribute与getAttribute
- IE6/7不支持setAttribute设置style / 不支持getAttribute获取style属性值
- JavaScript方法之getAttribute()和setAttribute()方法
- JS getAttribute和setAttribute(取得和设置属性)的使用介绍
- Javascript兼容性之——getAttribute(),setAttribute()(获取设置属性)
- js onchange事件,以及setAttribute,getAttribute
- JavaScript方法之getAttribute()和setAttribute()方法
- 有关getAttribute()方法和setAttribute()方法的说明
- getParameter() getAttribute() setAttribute()