The code copy is as follows:
function square(num){
var total = num*num;//Local variables
return total;
}
var total = 50;//Global variable
var number = square(20);
alert(total);//The result is 50
function square(num){
total = num*num;//Global variable
return total;
}
var total = 50;//Global variable
var number = square(20);
alert(total);//The result is 400
This subtle difference will affect the results of the program
The code copy is as follows:
body{
color:white;
background-color:black;
}
These colors not only act on content that is directly contained in the <body> tag, but also on all elements nested within the body element
The id attribute is like a hook, which connects to an element in the document on one end and to a style in the CSS style sheet on the other end.
document.getElementById("purchases") This call will return an object, which corresponds to a unique element in the document object, that element
The id attribute value is purchases
In fact, each element in the document is an object. Any object can be obtained using the method provided by DOM.
getElementsByTagName returns an array, and returns an array even if there is only one element in the entire document.
Example:
The code copy is as follows:
var items = document.getElementsByTagName("li");
for(var i = 0;i<items.length;i++){
alert(typeof items[i]);
}
All displayed information are object
getElementByClassName
Also returns an array of elements with the same class name
Get and set properties
getAttribute
object.getAttribute(attribute)
Note: The getAttribute method does not belong to the document object, it can only be called through the element node object.
setAttribute
object.setAttribute(attribute,value)
example:
var shopping = document.getElementById("purchases");
shopping.setAttribute("title","a list of goods");