Getting CLASS objects with native JS (simple and practical) _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces how to use native JS to obtain CLASS objects. Friends who have read dom programming art may know that it is the most commonly used .... I think of dom programming art.

   
  Untitled document    

Sss

Script function getElementsClass (classnames) {var classobj = new Array (); // defines the Array var classint = 0; // defines the Array subscript var tags = document. getElementsByTagName ("*"); // obtain all HTML tags for (var I in tags) {// traverse tags if (tags [I]. nodeType = 1) {// determine the node type if (tags [I]. getAttribute ("class") = classnames) // identifies and requires the same CLASS name, and forms an array {classobj [classint] = tags [I]; classint ++ ;}}return classobj; // returned array} // The following is the var a = getElementsClass ("ca"); a [0]. onclick = function () {alert ("");} a [1]. innerHTML = 'Here we are '; script

Sometimes multiple dom classes are the same and can be processed as follows:

The Code is as follows:


Var a = getElementsClass ("ca ");
For (var I = 0; I (function (I ){
Alert (a [I])
}) (I)
}


If there are multiple identical classes, you can use them together. If you want to achieve JQ, you can directly click the class. You also need to do some complicated processing.
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.