BOM and DOM operation learning

1. Foreplay

JavaScript is divided into ECMAScript, DOM and BOM. Some interactive functions of web pages use BOM and DOM related knowledge.

BOM (Browser Object Model) refers to the Browser Object Model, which enables JavaScript to "talk" with the browser.

DOM (Document Object Model) refers to the Document Object Model, through which all elements of HTML documents can be accessed.

Window object is one of the top-level objects of client JavaScript. Since window object is the common ancestor of most other objects, the reference of window object can be omitted when calling the methods and properties of window object. For example: window document.write() can be abbreviated as: document write().


2.1 window (important)

Window stands for browser window

window.alert(1)//Spring frame
window.innerHeight//Browser inner border height
window.innerwidth//Browser inner border width a new window
window.close()//Close current window
. . . 
//You can debug the browser window and try other

2.2 Navigator (just understand)

Navigator encapsulates the information of the browser

navigator.appName  // Full name of Web browser
navigator.appVersion  // Detailed string of Web browser manufacturer and version
navigator.userAgent  // Most client information
navigator.platform   // The operating system on which the browser is running

2.3 screen

Screen stands for screen size

screen.availWidth //Available screen width
screen.availHeight //Available screen heights

2.4. history object (just understand)

The history object contains the history of the browser. The browsing history object contains the user's browsing history of the current page, but we can't view the specific address. It can be simply used to move forward or backward a page.

history.forward()  // Forward one page
history.back()  // Back one page

2.5 location (important)

The location object is used to get the address (URL) of the current page and redirect the browser to a new page."The server"
location.reload()//Refresh page
location.assign()//Set new address


DOM (Document Object Model) is a set of methods to abstract and conceptualize the content of documents. When a web page is loaded, the browser creates a Document Object Model for the page. The HTML DOM model is constructed as a tree of objects.

HTML DOM tree: [external link image transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-nnt6d7ye-1646221695300) (C: \ users \ 86183 \ appdata \ roaming \ typora \ typora user images \ image-20220302155008141. PNG)]

The DOM standard specifies that each component in an HTML document is a node:

  • Document node (document object): represents the whole document
  • Element node (element object): represents an element (label)
  • Text node (text object): represents the text in the element (label)
  • Attribute node (attribute object): represents an attribute, and only elements (labels) have attributes
  • Comments are comment nodes (comment objects)

JavaScript can create dynamic HTML through DOM:

  • JavaScript can change all HTML elements in a page
  • JavaScript can change all HTML attributes in a page
  • JavaScript can change all CSS styles in a page
  • JavaScript can respond to all events in the page

3.1. Obtaining nodes

let h1 = document.getElementsByTagName('h1');
let p1 = document.getElementById('p1');
let p2 = document.getElementsByClassName('p2');
let father = document.getElementById('father');

let children = father.children;//Get all nodes under the parent node

3.2. Update node

<div id="id1">123</div>

    let id1 = document.getElementById('id1');

Operation text

id1.innerText='456' //Modify the content of the text
id1.innerHTML='<strong>123</strong>'  //HTML text tags can be parsed

Operation css

  • For CSS properties without horizontal lines, style is generally used directly Property name is enough
  • For CSS properties with middle horizontal line, replace the first letter after the middle horizontal line with uppercase (i.e. hump)

3.3. Create node

CreateElement (tag name)

    //1. Create a new node through Js
    let newP = document.createElement('p'); = 'newP';
    newP.innerText = 'hello';
    //2. Create a label node through setAttribute
    let myScript = document.createElement('script');

    //Create a style tag
    let myStyle = document.createElement('style');
    myStyle.innerHTML = 'body{background-color: yellowgreen;}';//Set label content


3.4. Insert node

1. append 2 insert

<p id="js">JavaScript</p>
<div id="list">
    <p id="se">JavaSE</p>
    <p id="ee">JavaEE</p>
    <p id="me">JavaME</p>

    //1. append
    let js = document.getElementById('js');
    let list = document.getElementById('list');
    list.appendChild(js);//Refers to appending js to the list
    //2. insert
    let ee = document.getElementById('ee');
    list.insertBefore(js,ee);//The js node is inserted in front of the ee node

3.5. Delete node

Step: get the parent node first, and then delete yourself through the parent node

<div id="father">
    <p id="p1">p1</p>
    <p class="p2">p2</p>
    let self = document.getElementById('p1');
    let parentSelf = p1.parentElement;

    //Deletion is a dynamic process
    father.removeChild(father.children[2]);//Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'.

Note: when deleting multiple nodes, children are always changing!

Keywords: Javascript Front-end Programming

Added by venradio on Wed, 02 Mar 2022 14:00:41 +0200