https://www.w3schools.com/js/js_htmldom.asp
講的蠻細的
Document Object Model
Javascript可以更換這個html的所有elements、attributes、style
Javascript可以移除html現有的elements和attributes
Javascript可以在html加上新的elements和attributes
Javascript可以反應在html上現有的event
Javascript可以在html頁面上新增事件
=====
將HTML的element視為物件,就會有propereties,methods,events
property => value, changing the content of an HTML element
method => action, add or delete an HTML element
innerHTML is a property
getElementByID is a method
=====
講的蠻細的
Document Object Model
Javascript可以更換這個html的所有elements、attributes、style
Javascript可以移除html現有的elements和attributes
Javascript可以在html加上新的elements和attributes
Javascript可以反應在html上現有的event
Javascript可以在html頁面上新增事件
=====
將HTML的element視為物件,就會有propereties,methods,events
property => value, changing the content of an HTML element
method => action, add or delete an HTML element
innerHTML is a property
getElementByID is a method
=====
document.appendChild(element) 常用
document.getElementById(id).onclick = function(){code} => event
The first HTML DOM Level 1 (1998), defined 11 HTML objects, object collections, and properties. These are still valid in HTML5.
=====
var x = document.querySelectorAll("p.intro"); => CSS Selector
=====
document.getElementById("p1").innerHTML = "New text!";
document.getElementById("myImage").src = "landscape.jpg";
=====
document.getElementById(id).style.property = new style
document.getElementById("p2").style.color = "blue";
<button type="button"
onclick="document.getElementById('id1').style.color = 'red'">
Click Me!</button> => events
onclick="document.getElementById('id1').style.color = 'red'">
Click Me!</button> => events
=====
function myMove() {
var elem = document.getElementById("animate");
var pos = 0;
var id = setInterval(frame, 5);
function frame() {
if (pos == 350) {
clearInterval(id);
} else {
pos++;
elem.style.top = pos + 'px';
elem.style.left = pos + 'px';
}
}
} => Animation
var elem = document.getElementById("animate");
var pos = 0;
var id = setInterval(frame, 5);
function frame() {
if (pos == 350) {
clearInterval(id);
} else {
pos++;
elem.style.top = pos + 'px';
elem.style.left = pos + 'px';
}
}
} => Animation
=====
- When a user clicks the mouse
- When a web page has loaded
- When an image has been loaded
- When the mouse moves over an element
- When an input field is changed
- When an HTML form is submitted
- When a user strokes a key
<h1 onclick="this.innerHTML = 'Ooops!'">Click on this text!</h1>
document.getElementById("myBtn").onclick = displayDate;
onload,onchange,onmouseover,onmousedown,onmouseup,onclick
=====
document.getElementById("myBtn").addEventListener("click", displayDate);
element.addEventListener(event, function, useCapture);
window.addEventListener("resize", function(){
document.getElementById("demo").innerHTML = sometext;
});
document.getElementById("demo").innerHTML = sometext;
});
Event Bubbling or Event Capturing?
=====
Node Tree
The nodeValue Property
The nodeValue property specifies the value of a node.
- nodeValue for element nodes is undefined
- nodeValue for text nodes is the text itself
- nodeValue for attribute nodes is the attribute value
=====
var element = document.getElementById("div1");
element.appendChild(para);
element.appendChild(para);
=====
HTML DOM Node List
=====
留言
張貼留言