JavaScript sets the methods to get and set properties

  • 2020-05-10 17:44:15
  • OfStack

getAttribute

This method is used to get the attributes of the element. The method is called as follows:


object.getAttribute(attribute)

Unlike some of the methods described earlier, the getAttribute method does not belong to the document object, so it cannot be called through the document object. It can only be invoked through the element node object.

This method takes only one argument, and you specify the name of the property to query. If the specified property is not set, the result returns the null object.

setAttribute

The opposite of the above is setAttribute, which is used to set the attributes of the element nodes. The method of invocation is as follows:


object.setAttribute(attribute)

This method takes only one parameter, the property you want to set.

Develop reading

Once the document has been modified by setAttribute, you will still see the original value when viewing the document's source code through the browser's view source (view source code) option, meaning that the changes made by setAttribute will not be reflected in the source code of the document itself. This phenomenon of "not 1 in table" comes from the working mode of DOM: the static content of the document is loaded first, and the static content of the document is not affected during dynamic refresh. That's the real power of the DOM: refreshing the content without refreshing the page in the browser.

The above two methods belong to the new API in DOM Level 1. Before they appear, they can be implemented by another method, as shown below

Get properties:


var val = element.attribute // Retrieve attributes

This is the same thing as


var val = element.getAttribute('attribute');  

Set properties:


element.attribute = "the new value";

It is equivalent to


element.setAttribute("attribute", "the new value");   

This is recommended if you want to slacking off, but the best practice is to stick with DOM, which means using setAttribute and getAttribute.

The above is the entire content of this article, there is a need to learn the small partners, I hope you can like.


Related articles: