js中對節點屬性的操作和對節點的操作


常用的節點屬性操作方法

1.setAttribute(name,value):給某個節點添加一個屬性

2.getAttribute(name):獲取某個節點屬性的值。

3.removeAttribute(name):刪除某個節點的屬性。

例:

    window.onload = function(){
        //查找body節點
        var node_body = document.body;
        //alert(node_body);
        //查找img節點
        var imgObj = node_body.firstChild;
        //增加src屬性
        imgObj.setAttribute("src","./img/1.jpg");
        imgObj.setAttribute("width","200");
        imgObj.setAttribute("border","2");
        imgObj.setAttribute("style","cursor:pointer");
        //刪除屬性
        imgObj.removeAttribute("border");
   }

對節點的操作

//節點的創建
  //createElement(tagName):創建一個指定的HTML標記

                        tagName:是指不帶尖括號的HTML標記名稱

                          例:var imgObj = document.createElement();
  //appendChild(elementObj):將創建的節點,追加到某個父節點下

             elementObj是創建的一個子節點

                 例:document.body.appendChild(imgObj)
  //removeChile(elementObj):刪除子節點

  //網頁加載完成后

例:
  window.onload = function(){
            //創建一個<img>標記
            var imgObj = document.createElement("img");
           //增加屬性
           imgObj.setAttribute("src","./img/1.jpg");
           imgObj.setAttribute("width","700");
           //將創建的圖片節點
           document.body.appendChild(imgObj);
           

  }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM