JavaScript settings how to get and set properties

Source: Internet
Author: User

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

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.