標籤:public script ima setattr cli window new stc rem
<!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> new document </title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <meta name="author" content="" /> <style type="text/css"></style> <script type="text/javascript"> //當網頁載入完成調用js程式 //匿名函數:就是沒有名字的函數,不能單獨定義,也不能單獨調用 //匿名函數只能作為資料傳給其他變數 window.onload=function(){ //尋找img節點 var imgObj=document.body.firstChild; //添加src屬性 imgObjAttribute("src","images/01.jpg"); //添加width屬性 imgObj.setAttribute("width","500"); //添加border屬性 imgObj.setAttribute("border",2); //添加onclick屬性 imgObj.setAttribute("onclick","removeImg(this)"); } //刪除src屬性 function removeImg(imgObj){ imgObj.removeAttribute("src"); } /*匿名函數調用和作為資料//給變數賦不同的值,則變數就是不同類型//給變數賦一個函數,則變數就是函數型 var a="abc"; var a=[10]; var a= function(){ alert("ok"); } //調用方法: a(); */ </script> </head> <body> <body><img onclick="removeImg(this)" /></body> </body></html>
html節點屬性操作