How to get and set properties in JavaScript settings _ javascript skills

Source: Internet
Author: User
This article describes how to obtain and set attributes in JavaScript settings. learn how to use getAttribute and setAttribute. For more information, see GetAttribute

This method is used to obtain the attributes of an element. The Calling method is as follows:

The code is as follows:


Object. getAttribute (attribute)

The methods described earlier are different. the getAttribute method does not belong to the document object, so it cannot be called through the document object. It can only be called through element node objects.

This method only accepts one parameter. you specify the name of the attribute to be queried. If the specified property is not set, a null object is returned.

SetAttribute

The opposite is setAttribute. this method is used to set attributes of element nodes. The call method is as follows:

The code is as follows:


Object. setAttribute (attribute)

This method only accepts one parameter, that is, the attribute you want to set.

Additional reading

After the document is modified through setAttribute, when you view the source code of the document through the view source (view source code) option of the browser, the previous value will be changed, that is, the changes made by setAttribute are not reflected in the source code of the document. This "table is different" phenomenon comes from the DOM working mode: loading static content of the document first, and refreshing it dynamically without affecting the static content of the document. This is the real power of DOM: refreshing the page content does not require refreshing the page in the browser.

The above two methods belong to the APIs added in DOM Level 1. before they appear, they can be implemented through another method, for example:

Get attributes:

The code is as follows:


Var val = element. attribute // Get attributes

The above is equivalent

The code is as follows:


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

Set attributes:

The code is as follows:


Element. attribute = "the new value ";

It is equivalent

The code is as follows:


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

If you want to be lazy and press on the keyboard, we recommend the above method, but the best practice is to respect DOM standards, that is, using setAttribute and getAttribute.

The above is all the content of this article. if you have any friends who need it, I hope you will enjoy it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.