(Original example) How to Implement getelementbyclassname ()? (Web) (JavaScript)

Source: Internet
Author: User

Abstract
W3C dom does not have document. getelementbyclassname (), but sometimes there is no ID, and how can we fix it when only the class name can be captured?

Introduction
During blog rebuild, there is often no ID to be captured, but only the class to be captured, but it is a pity that W3C dom does not have a document. getelementbyclassname (). The following javascript can modulo getelementbyclassname ().

Javascript/getelementbyclassname

1 Function Getelementsbyclassname (classname ){
2 VaR All = Document. All ? Document. ALL: Document. getelementsbytagname ( ' * ' );
3 VaR Elements =   New Array ();
4 For ( VaR E =   0 ; E < All. length; e ++ ){
5 If (ALL [e]. classname = Classname ){
6 Elements [elements. Length] = All [E];
7 Break ;
8 }
9 }
10 Return Elements;
11 }
12

2 rows

VaR All = Document. All ? Document. ALL: Document. getelementsbytagname ( ' * ' );

Document. all is ie4's DHTML technical support. ie4 is followed by support, so it can be used separately to determine whether document is supported. all determines whether the struct is ie. If it is not ie, the W3C Dom document is used. getelementbytagname ('*') replaces all, so this method can be used in IE or Firefox.

4 rows ~ 9 rows

For ( VaR E =   0 ; E < All. length; e ++ ){
If (ALL [e]. classname = Classname ){
Elements [elements. Length] = All [E];
Break ;
}
}

When a tag is circled and a tag is used to determine whether the class name actually meets the condition, the first matching condition will jump out of the condition circle, in this way, you can find the first tag that matches the class name.

Conclusion
In addition to Google Adsense Link, I use this method. Because no ID is available, I have to find the class name. To join the method, please refer to the test (original example) how can I trigger a Google Adsense advertisement in a blog? (Web) (CSS) (JavaScript ).

See also
(Original) How can I trigger a Google Adsense advertisement in a blog? (Web) (CSS) (JavaScript)
Why does W3C Dom not provide insertafter? (Web) (JavaScript)

Reference
Getelementbyclassname of a kubernetes ga

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.