BOM:
- Concept: Browser Object Model Browser Object Model
- Encapsulate the components of the browser into objects.
-
Form:
- Window: Window object
- Navigator: Browser Object
- Screen: Display screen object
- History: Historical Record Object
- Location: Address Bar Object
-
Window: Window object
Establish
-
Method
-
Methods related to pop-up boxes:
alert() displays a warning box with a message and a confirmation button. A kind of
confirm() displays a dialog box with a message and confirmation and cancellation buttons.* If the user clicks the OK button, the method returns true * If the user clicks the Cancel button, the method returns false
prompt() displays a dialog box that prompts the user for input.
Return value: Gets the value entered by the user -
Method related to opening and closing:
==close == Close the browser window.- Who calls me, who I shut down?
==open == Open a new browser window
- Return a new Window s object
-
Ways related to timers
==setTimeout() == Call a function or evaluate an expression after a specified number of milliseconds.* Parameters: 1. js code or method object 2. Millisecond value * Return value: Unique identifier used to cancel the timer
clearTimeout() cancels timeout set by the setTimeout() method.
setInterval() calls a function or evaluates an expression in milliseconds for a specified period.
==clearInterval() == Cancel timeout set by setInterval(). -
- Attributes:
- Get other BOM objects:
history
location
Navigator
Screen: - Getting DOM objects
document
- Get other BOM objects:
- Characteristic
- Windows objects do not need to be created and can be used directly by Windows. window. method name ();
- Windows references can be omitted. Method name ();
-
Location: Address Bar Object
- Create (obtain):
- window.location
- location
- Method:
- reload() reloads the current document. Refresh
- attribute
- href sets or returns the full URL.
==case==
- Create (obtain):
-
History: Historical Record Object
- Create (obtain):
- window.history
- history
- Method:
- back() loads the first URL in the history list.
- forward() loads the next URL in the history list.
- Go (parameter) loads a specific page in the history list.
- Parameters:
- Positive Number: Several Historic Records Forward
- Negative Number: Backward Several Historic Records
- Parameters:
- Attributes:
- length returns the number of URL s in the current window history list.
- Create (obtain):
DOM:
-
Concept: Document Object Model Document Object Model
- The components of markup language documents are encapsulated as objects. These objects can be used to perform CRUD dynamic operations on markup language documents
-
The W3C DOM standard is divided into three different parts:
- Core DOM - Standard model for any structured document
- Document: Document object
- Element: Element object
- Attribute: Attribute object
- Text: Text object
- Comment: Annotation object
- Node: Node object, the other five parent objects
- XML DOM - Standard Model for XML Documents
- HTML DOM - Standard Model for HTML Documents
- Core DOM - Standard model for any structured document
-
Core DOM model:
-
Document: Document object
- Create (get): In the html dom model, you can use window s objects to get
- window.document
- document
- Method:
- Get Element objects:
- getElementById(): Gets the element object based on the id attribute value. id attribute values are generally unique
- getElementsByTagName(): Get element objects by element name. The return value is an array
- getElementsByClassName(): Get the element objects based on the value of the Class attribute. The return value is an array
- getElementsByName(): Get the element objects based on the name attribute value. The return value is an array
- Create other DOM objects:
createAttribute(name)
createComment()
createElement()
createTextNode()
- Get Element objects:
- attribute
- Create (get): In the html dom model, you can use window s objects to get
-
Element: Element object
- Acquisition/creation: Acquisition and creation through document
- Method:
- removeAttribute(): Delete attributes
- setAttribute(): Set properties
-
Node: Node object, the other five parent objects
- Feature: All dom objects can be considered as a node
- Method:
- CRUD dom tree:
- appendChild(): Adds a new child to the end of the node's list of child nodes.
- removeChild(): Delete (and return) the specified child node of the current node.
- replaceChild(): Replace a child node with a new node.
- CRUD dom tree:
- Attributes:
- parentNode returns the parent of the node.
==case==
-
-
HTML DOM
- Setting and Acquisition of Label Body: innerHTML
- Using attributes of html element objects
- Control element style
- Use the style attribute of the element to set it
Such as:
// Modify Style Mode 1
div1.style.border = "1px solid red";
div1.style.width = "200px";
//font-size--> fontSize
div1.style.fontSize = "20px"; - Define the style of the class selector in advance, and set its class attribute value through the element's className attribute.
- Use the style attribute of the element to set it
Event monitoring mechanism:
- Concept: After certain components are executed, some code execution is triggered.
- Event: Some operations. For example: click, double-click, press the keyboard and move the mouse
- Event Source: Components. For example, the button text input box.
- Listener: Code.
- Registered listener: Combines events, event sources, and listeners. When an event occurs on the event source, it triggers the execution of a listener code.
- Common events:
- Click Events:
- onclick: click event
- ondblclick: Double-click event
- Focus events
- onblur: losing focus
- onfocus: Elements get focus.
- Load events:
- onload: A page or an image is loaded.
- Mouse events:
- The onmousedown mouse button is pressed.
- The onmouseup mouse button is released.
- The onmousemove mouse is moved.
- Move the onmouseover mouse over an element.
- The onmouseout mouse moves away from an element.
- Keyboard events:
- onkeydown A keyboard key is pressed.
- One of the keyboard keys on Keyup is released.
- onkeypress A keyboard key is pressed and released.
- onkeydown A keyboard key is pressed.
- Selection and change
- The content of the onchange domain is changed.
- onselect text is selected.
- Form events:
- The onsubmit confirmation button is clicked.
- The onreset reset button is clicked.
- Click Events:
DOM Event Binding
- Directly on the html tag, specify the attributes (operations) of the event, and the value of the attributes is the js code
- Event: onclick -- click event
- Get element objects through js, specify event attributes, and set a function
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <img id="myImage1" src="img/off.gif"/> </body> <script type="text/javascript"> function changeSrc2() { alert("Or me?") } var myImage1 = document.getElementById("myImage1"); myImage1.onclick = changeSrc2; </script> </body> </html>
Case lights on
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Lamp switch</title> </head> <body> <img id="light" src="img/off.gif"/> <script> /* Analysis: 1.Getting Picture Objects 2.Binding Click Events 3.Switch pictures with each click * Rules: * If the light is on, switch the picture to off. * If the light is off, switch the picture to on * Complete with flag Tags */ //Getting objects var light = document.getElementById("light"); var flag = false; //Binding Click Events light.onclick = function (ev) { if (!flag) { light.src = "img/on.gif"; flag = true; } else { light.src = "img/off.gif"; flag = false; } } </script> </body> </html>
Open a new window
<input type="button" value="click" name="bt" id="bt"> <script> // var flag = window.confirm("Make sure to delete! ""; // alert(flag); // var a= prompt("Please enter a user name"); // alert(a); var as= document.getElementById("bt"); as.onclick=function () { open("https://www.baidu.com";//Open a new window } </script>
Close the newly opened window
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <input type="button" value="open" name="bt" id="bt"> <input type="button" value="Close" name="bt2" id="bt2"> <script> // var flag = window.confirm("Make sure to delete! ""; // alert(flag); // var a= prompt("Please enter a user name"); // alert(a); var as = document.getElementById("bt"); var newWindows; as.onclick = function () { newWindows = open("https://www.baidu.com";//Open a new window } var as1 = document.getElementById("bt2"); as1.onclick = function () { newWindows.close();//Close } </script> </body> </html>
Disposable timer
setTimeout(fun, 3000); function fun() { alert("boom") }
setInterval Cyclic Timer
setInterval(fun,2000) function fun() { alert("boom") }
Cancel timer
var a = setInterval(fun, 2000) function fun() { alert("boom") } clearInterval(a);
Return to home page in 5 seconds
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>automatic skip</title> </head> <body> <p align="center"> <span id="num" style="color: red">5</span>After seconds, automatically jump to the home page... </p> <script> var num1 = 5; var n = document.getElementById("num"); var as = setInterval(fun, 1000) function fun() { num1--; if (num1 == 0) { location.href = "https://www.baidu.com" clearInterval(); } else n.innerHTML=num1+""; } </script> </body> </html>
Remove adding new nodes
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <style> div{ border: red solid 1px; } #div1{ width: 200px; height: 200px; } #div2{ width: 100px; height: 100px; } #div4{ width: 100px; height: 100px; } </style> <body> <div id="div1">div1 <div id="div2">div2</div> </div> <br> <br> <br> <a id="delete" href="javascript:void(0);"> Delete child nodes</a> <a id="add" href="javascript:void(0);"> Add child nodes</a> <script> var byId = document.getElementById("delete"); byId.onclick = function () { var byId1 = document.getElementById("div1"); var byId2 = document.getElementById("div2"); byId1.removeChild(byId2); } /** * Hyperlink function: 1.Can be clicked: Style 2.Click and jump to the url specified by href Requirements: Retain 1 function and remove 2 functions Implementation: href="javascript:void(0);" */ var byId_add = document.getElementById("add"); byId_add.onclick = function () { var byId1 = document.getElementById("div1"); var div3= document.createElement("div"); div3.setAttribute("id","div4") byId1.appendChild(div3); } </script> </body> </html>