[JavaScript] Getelementsbyclassname with classname and getattribute!

Source: Internet
Author: User

The problem is not supported for getelementsbyclassname in JavaScript in IE 6/7/8.

Then the need to simulate the getelementsbyclassname need to use the ClassName attribute, which involves JavaScript getattribute problem.

In IE 6/7, there is a bug for getattribute

It is necessary to use ClassName to obtain such as the following:

var node = document.getElementById ("test"); var name = Node.classname;

For standard browsers, the ability to use ClassName directly can also use Getattritbute, as a result:

Node.getattritbute ("class");

However, if it is IE 6/7, there is a problem with getattribute ().

So getelementsbyclassname can use the following methods, for example:

Getelementsbyclazzname = Document.getelementsclassname? function (name) {return document.getelementsclassname (name);}:function (name) {var nodes = document.getElementsByTagName ("*"), result = [];for (var node in nodes) {if (Node.classname && Node.className.indexOf (name)) {Result.push (node);}} return result;}

This is a simpler way to implement.

1: General properties recommended: Node.xxx.
2: Own definition attribute recommended: Node.getattribute ("xxxx").
3: It is recommended to use Node.getattribute ("xxx") when getting the target of keyword in JavaScript. such as the for in the label.
4: When acquiring the target is reserved word, such as: class, please use ClassName instead.

[JavaScript] Getelementsbyclassname with classname and getattribute!

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.