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