js教程系列33 :javascript-DOM節點屬性
阿新 • • 發佈:2018-04-26
name AR set ons alt bbb aaa win ttr
1 設置節點屬性三個方法:
獲取:getAttribute(名稱)
設置:setAttribute(名稱, 值)
刪除:removeAttribute(名稱)
舉個例子:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script> window.onload = function () { var eleNode = document.getElementsByTagName("img")[0];//1.元素節點.屬性或者元素節點[屬性] eleNode.src = "image/aaa.png"; eleNode.aaa = "bbb"; console.log(eleNode.aaa); console.log(eleNode.src); console.log(eleNode.tagName); console.log(eleNode["title"]); console.log(eleNode["className"]); console.log(eleNode["alt"]); //2.元素節點.方法(); console.log(eleNode.getAttribute("id")); eleNode.setAttribute("id","你好"); eleNode.setAttribute("ccc","ddd"); eleNode.removeAttribute("id"); } </script> </head> <body> <img src="image/aaa.png" class="box" title="圖片" alt="aaa" id="aaa"/> </body> </html>
js教程系列33 :javascript-DOM節點屬性