This article mainly introduces how to obtain and set attributes in js. If you need a friend, please refer to it and hope to help you.
The Code is as follows:
Function square (num ){
Var total = num * num; // local variable
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 program results.
The Code is as follows:
Body {
Color: white;
Background-color: black;
}
These colors not only apply to those directly contained inThe content in the tag also acts on all elements nested in the body element.
The id attribute is like a hook. It is connected to an element in the document and a style in the CSS style table.
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 of is purchases.
In fact, each element in the document is an object. Use the methods provided by DOM to obtain any object.
GetElementsByTagName returns an array even if there is only one element in the document.
Example:
The Code is as follows:
Var items = document. getElementsByTagName ("li ");
For (var I = 0; I Alert (typeof items [I]);
}
The display information is all objects.
GetElementByClassName
Returns an array of elements with the same class name.
Get and set attributes
GetAttribute
Object. getAttribute (attribute)
Note: The getAttribute method is not a document object and can only be called through an Element Node object.
SetAttribute
Object. setAttribute (attribute, value)
Example:
Var shopping = document. getElementById ("purchases ");
Shopping. setAttribute ("title", "a list of goods ");