DOM search and operation (document) _js

1. DOM operation

DOM is the document operation, is to find the html tag corresponding to the operation

Second, find the label

1. Direct search

document.getElementById Find according to the tag id attribute

document.getElementsByName searches according to the name attribute of the tag

document.getElementsByClassName searches according to the class attribute

document.getElementsByTagName to search by tag name

2. Indirect search

parentElement parent node label

chlidren: all subtags

firstElementChild first word label

lastElementChild last child tag

nextElementSibling next brother tag

previousElementSibling Previous brother tag

 

 

3. Operation label

1. Text operations

tag.innerText manipulate text content

tag.innerHtml operates everything inside

tag.value The value of the operation tag

2. Style operation

tag.className = "" directly operate the whole

tag.classList operation on the list of classes

  tag.classList.add ('style name') add style

  tag.classList.remove ('style name') delete style

3.style operation

tag.style.fontSize = '16px' Directly operate on the style attribute

4. Attribute operation

tag.setAttribute ('Attribute name', 'Attribute value') Add attribute

tag.getAttribute ('Attribute name') Get attribute

tag.removeAttribute ('Attribute name') delete attribute

tag.attributes Get all attributes

 

Guess you like

Origin www.cnblogs.com/chenxiaozan/p/12683438.html