DOM 节点属性,增删改节点属性

来源:互联网 发布:淘宝创业故事 编辑:程序博客网 时间:2024/05/22 01:11
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="text/javascript">//匿名函数:就是没有名字的函数。//匿名函数,不能单独定义,也不能单独调用//匿名函数,只能作为数据传给其它变量//程序顺序执行,必须当网页加载完成才调用JS程序,所以放到onload函数中。window.onload = function(){//查找img节点var imgObj = document.body.firstChild;//添加src属性imgObj.setAttribute("src","images/01.jpg");//添加width属性imgObj.setAttribute("width",400);//添加border属性imgObj.setAttribute("border",2);//添加style属性imgObj.setAttribute("style","padding:20px");//添加onclick属性imgObj.setAttribute("onclick","removeImg(this)");}//函数:删除src属性function removeImg(imgObj){//删除src属性imgObj.removeAttribute("src");imgObj.removeAttribute("width");}</script></head><body><img /></body></html>

原创粉丝点击