IE 호환 getElementsByClassName () 방법

3380 단어 element
IE8 및 다음 getElementsByClassName () 호환 방법
 1 
 2     function getElementsByClassName = function(className, tagName) {
 3         // getElementsByClassName() 
 4         if(document.getElementsByClassName){
 5             return document.getElementsByClassName(className)
 6         }
 7         // document 
 8         var children = document.getElementsByTagName(tagName);
 9         // class 
10         var elements = new Array();
11      
12         for (var i = 0; i < children.length; i++) {
13             var child = children[i];
14             var classNames = child.className.split(' ');            // class 
15             for (var j = 0; j < classNames.length; j++) {
16                 if (classNames[j] == className) {
17                     elements.push(child);
18                     break;
19                 }
20             }
21         }
22         return elements;
23     };

좋은 웹페이지 즐겨찾기