JavaScript Learning path elements get and set properties

Source: Internet
Author: User

Pick up the mood, learn to learn js! Summarize your own study income!

There are three ways to get the nodes of an element, respectively, by the element ID, by the signature and the class name.

1.GetElmentById: Returns an object corresponding to the element node that has the value of the given ID attribute, with only one argument, and the value of the id attribute of the element must be in single or double quotes

Documrnt. Getelmentbyid (ID)

2.GetElmentsByTagName: The method returns an array of objects, each corresponding to an element in the document that has a given tag, whose argument is the name of the label

Dovument. Getelmentsbytagname (TAG)

3.GetElmentsByClassName: Returns an array of elements with the same class name, accepting only one parameter, that is, the class name

Document. Getelmentsbyclassname (Class)

Get and Set properties

1.getAttribute: is a function, he has only one parameter---intended to query the name of the property, he can only be called through the element node object, can only be used for element nodes. For example, he can be used with the Getelmentsbytagname method to get the title property of each <p> element

var paras=document. Getelmentsbytagname ("P");  for (var i=0;i<paras.length;i++) {     alert (Paras[i].getattribute ("title"));  }

2.setAttribute: Run changes to the value of the attribute node and can only be used for element nodes.

Object.setattribute (Attribute,value);

Here's something to note: After making changes to the document through SetAttribute, and then viewing the document source through the browser's view source option, it is still the value of the property before the change. In other words, the changes made by setattribute are not reflected in the source code of the document, which is the source of the DOM work mode, loading the static content of the document, dynamic refresh, dynamic refresh does not affect the static content of the documents, that is, the content of the page will not need to refresh the page in the browser

JavaScript Learning path elements get and set properties

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.