【javascript總結】javascript的DOM編程,javascriptdom編程

來源:互聯網
上載者:User

【javascript總結】javascript的DOM編程,javascriptdom編程
訪問HTML元素方式

  • 根據ID訪問HTML元素。
  • 根據節點關係訪問HTML元素。

常用的有

document.getElementById("idVal");//得到ID名字為idVal的節點。
document.getElementsByName("name");//返回的是name屬性為name的標籤集合
document.getElementsByTagName("input")//返回標所有的input標籤的集合
DOM的增刪改查增:

1.建立節點

 document.createElement(Tag) //其中Tag必須是一個合法的HTML標籤名字

複製節點

Node cloneNode(boolean deep) //當deep為true的時候表示複製當前節點的時候,也複製裡面的全部內容 否則是複製她自己

添加節點

 appendChild(Node newNode)//添加為最後一個節點 insertBefore(Node newNode, Node refNode)//添加到refNode節點的前面 replaceChild(Node newNode, Node oldNode)//替換節點
刪:

刪除節點

removeChild(oldNode)// 刪除oldNode節點  參數可以使對象也可以是數字

得到要修改的元素然後修改innerHTML或者其value。

訪問節點

document.getElementById("id");//得到id為id的元素

還可以使用節點之間的兄弟關係。

訪問表單

document.getElementById('form').elements[0] //返回的是表單中的第一個元素。document.getElementById('form').elements['pass']//返回name為pass的表單元素。

訪問列表框

document.getElementById('select').options[n] //挑選清單中的第n個元素。

訪問表格元素

document.getElementById('table').row[1].cells[2]//返回表格的第2行的第三格。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.