This article mainly describes the JavaScript settings to get and set the properties of the method, learn to use GetAttribute, setattribute usage, the need for friends can refer to the following
GetAttribute
The method is used to get the attributes of an element, as shown in the following way:
Copy CodeThe code is 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 called 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 property to query. If the specified property is not set, the result returns a null object.
SetAttribute
In contrast to the above action is setattribute, which is used to set the attributes of the element node. The invocation method is as follows:
Copy CodeThe code is as follows:
Object.setattribute (attribute)
The method accepts only one parameter, which is the property you want to set.
Expand Reading
After making changes to the document through SetAttribute, you will still see the pre-change value when you view the source code of the document through the browser's view sources option (the view feed), which means that the changes made by setattribute are not reflected in the source code of the document itself. This "duplicity" phenomenon stems from the DOM's working pattern: loading the static content of the document, dynamic refresh, and dynamic refresh does not affect the static content of the document. This is the true power of the DOM: refreshing the content of the page without having to refresh the page in the browser.
The above two methods belong to the new API in DOM Level 1, which can be implemented in a different way before they appear, for example:
Get Properties:
Copy CodeThe code is as follows:
var val = element.attribute//Get Property
The above is equivalent to
Copy CodeThe code is as follows:
var val = element.getattribute (' attribute ');
To set properties:
Copy CodeThe code is as follows:
Element.attribute = "The new value";
It is equivalent to
Copy CodeThe code is as follows:
Element.setattribute ("attribute", "the new Value");
If you want to be lazy and don't hit the keyboard, then recommend the above approach, but best practice is to promote the DOM standard, that is, the use of setattribute and getattribute.
JavaScript settings how to get and set properties