Home > Web Front-end > JS Tutorial > js method of getting and setting attributes_javascript skills

js method of getting and setting attributes_javascript skills

WBOY
Release: 2016-05-16 16:58:57
Original
933 people have browsed it

Copy code 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 result of the program
Copy code The code is as follows:

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

These colors will not only apply to content directly contained in the tag, but will also apply to all elements nested within the body element

The id attribute is like a hook. One end is connected to an element in the document, and the other end is connected to a certain style in the CSS style sheet

document.getElementById("purchases") This call will return an object. This object corresponds to a unique element in the document object. The id attribute value of that element
is purchases

Actually, every element in the document is an object. Any object can be obtained using the methods provided by the DOM.

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

Example:

Copy code The code is as follows:

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

display Information is all object

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

Getting and setting attributes
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");

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template