-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09_DOM.js
More file actions
88 lines (55 loc) · 3.08 KB
/
Copy path09_DOM.js
File metadata and controls
88 lines (55 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Window Object
// The window object represents an open window in a browser. It is browser’s object (not JavaScript’s)
// & is automatically created by browser.
// It is a global object with lots of properties & methods.
// What is DOM?
// When a web page is loaded, the browser creates a Document Object Model (DOM) of the page
// The DOM is a tree-like structure that represents the elements of the web page as objects. Each element in the HTML document is represented as a node in the DOM tree, and these nodes can be accessed and manipulated using JavaScript.
// DOM Manipulation :-
// Selecting with id
// document.getElementById(“myId”)
// Selecting with class
// document.getElementsByClassName(“myClass”)
// Selecting with tag
// document.getElementsByTagName(“p”)
// Query Selector :-
// document.querySelector(“#myId / .myClass / tag”)
//returns first element
// document.querySelectorAll(“p”)
// Properties :-
// tagName : returns tag for element nodes
// innerText : returns the text content of the element and all its children
// innerHTML : returns the plain text or HTML contents in the element
// textContent : returns textual content even for hidden elements
// Attributes :- Attributes are properties of an element that provide additional information about the element. They are defined in the HTML markup and can be accessed and manipulated using JavaScript.
// id : a unique identifier for the element
// class : a space-separated list of classes for the element
// src : the URL of an image or other media element
// href : the URL of a link element
// alt : alternative text for an image element
//getAttribute(attrbitueName) : returns the value of the attribute
//setAttribute(attributeName, attributeValue) : sets the value of the attribute
//removeAttribute(attributeName) : removes the attribute from the element
// Style :-
// node.style : returns the inline style of the element as a CSSStyleDeclaration object. You can use this object to get or set individual style properties.
node.style.property // returns the value of the property
node.style.property = value // sets the value of the property
// Insert Element :-
let el = document.createElement("div")
node.append( el ) //adds at the end of node (inside)
node.prepend( el ) //adds at the start of node (inside)
node.before( el ) //adds before the node (outside)
node.after( el ) //adds after the node (outside)
// Delete Element
node.remove( ) //removes the node
// Methods :-
// createElement() : creates an element node
//createTextNode() : creates a text node
//createComment() : creates a comment node
//createDocumentFragment() : creates a document fragment node
//appendChild() : appends a node to the end of the list of children of a specified parent node
//insertBefore() : inserts a node before a reference node as a child of a specified parent node
//removeChild() : removes a child node from the DOM
//replaceChild() : replaces a child node of a specified parent node with a new node
//cloneNode() : clones a node
//contains() : returns true if the specified node is a descendant of the current node