GetAttribute
This method is used to get the attributes of an element, which is called as follows:
Copy Code code as follows:
Object.getattribute (attribute)
Unlike some of the methods described earlier, the GetAttribute method is not part of the document object, so it cannot be invoked through the Document object. It can only be invoked through an element node object.
The method accepts only one parameter, and you specify the name of the attribute to query. If the specified property is not set, the result returns a null object.
SetAttribute
In contrast to the above, setattribute is used to set the attributes of the element node. The calling method looks like this:
Copy Code code as follows:
Object.setattribute (attribute)
The method accepts only one parameter, which is the property you want to set.
Expand your Reading
After you have modified the document through SetAttribute, you will still see the value before you change the source of the document through the browser's view source option, that is, the changes made by setattribute will not be reflected in the source code of the document itself. This "duplicity" phenomenon derives from the working mode of the DOM: loading the file's static content first, dynamically refreshing, and dynamic refreshing without affecting the static content of the document. This is the true power of DOM: refreshing the content of the page without having to refresh the page in the browser.
The above two methods are part of the new API in DOM Level 1, which can be implemented in another way before they appear, for example
Get Properties:
Copy Code code as follows:
var val = element.attribute//Get Properties
The above is equivalent to
Copy Code code as follows:
var val = element.getattribute (' attribute ');
Set properties:
Copy Code code as follows:
Element.attribute = "The new value";
It is equivalent to
Copy Code code as follows:
Element.setattribute ("attribute", "the new Value");
If you want to slack off the keyboard, then recommend the above approach, but the best practice is to promote the DOM standard, that is, the use of setattribute and getattribute.
The above is the entire content of this article, there is a need for small partners to learn, I hope we can enjoy.