Js method to get and set properties

  • 2020-03-30 02:05:19
  • OfStack


function square(num){
    var total = num*num;//A local variable
    return total;
}
var total = 50;//The global variable
var number = square(20);
alert(total);//The results of 50
function square(num){
    total = num*num;//The global variable
    return total;
}
var total = 50;//The global variable
var number = square(20);
alert(total);//The results of 400

This subtle difference can affect the outcome of the program

body{
    color:white;
    background-color:black;
}

These colors not only act on those directly contained in < Body> The contents of the tag will also apply to all elements nested within the body element

The id attribute is like a hook, with one end attached to an element in the document and another end attached to a style in the CSS style sheet

Document. The getElementById (" purchases ") this call returns an object, the object corresponds to a unique element in the document object, the element
The id attribute value for purchases

In fact, each element in the document is an object. Any object can be obtained using the methods provided by DOM.

GetElementsByTagName returns an array, even if there is only one element in the entire document.

Example:


var items = document.getElementsByTagName("li");
for(var i = 0;i<items.length;i++){
    alert(typeof items[i]);
}

The display information is all object

getElementByClassName
Also returns an array of elements with the same class name

Gets and sets properties
The getAttribute
Object. The getAttribute (attribute)
Note: the getAttribute method is not part of the document object and can only be invoked through the element node object.

The setAttribute
Object. The setAttribute (attribute, value)

Example:
Var shopping = document. GetElementById (" purchases ");
Shopping. SetAttribute (" title ", "a list of goods");


Related articles: