码迷,mamicode.com
首页 > Web开发 > 详细

JS DOM编程艺术——setAttribute—— JS学习笔记2015-7-7(第78天)

时间:2015-07-08 00:11:11      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

getAttribute & setAttribute 获取和设置属性

getAttribute 获取属性:

语法:object.getAttribute(attribute);   这个方法不属于document对象,所以不能使用document对象调用,只能通过元素节点对象调用;后面的setAttribute也是这样;

比如;var paras = document.getElementsByTagName(‘p‘);

        alert(paras[0].getAttribute(‘title‘))

 

setAttribute 修改属性;

object.setAttribute(attribute,value);

实例:var shopping = document.getElementById(‘purchases‘);

        shopping.setAttribute(‘title‘,"a list of goods");

tips:属性也就是包括title、alt、class、id等这些类别,所以可以通过上面这2个方法来获取和设置页面中元素的相应属性,比如改变样式等;

JS DOM编程艺术——setAttribute—— JS学习笔记2015-7-7(第78天)

标签:

原文地址:http://www.cnblogs.com/zhangxg/p/4628706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!