JavaScript settings how to get and set properties _javascript tips

Source: Internet
Author: User

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.

Related Article

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.