📂 JAVASCRIPT/그외
[ 미완성 ] getElementsByClassName 🕔 2017. 2. 28. 20:59
incompleted - getElementsByClassName
if ( ! document.getElementsByClassName ){
(function (){
document.getElementsByClassName = function ( className ){
// if ( this.querySelectorAll ){ return this.querySelectorAll( "." + className ); }
var children = this.getElementsByTagName( "*" );
var object = { length: 0 };
var count = 0, contain = false, x, n, child, classes;
for ( x = 0; x < children.length; x++ ){
child = children[ x ];
classes = child.className.split( / +/g );
for ( n = 0; n < classes.length; n++ ){ if ( classes[ n ] == className ) contain = true; }
if ( contain ){ object[ count ] = child; count++; }
contain = false;
}
object.length = count;
return object;
};
document.getElementsByClassName.toString = function (){ return "function getElementsByClassName() { [native code] }" };
var currentWindow = document.defaultView ? document.defaultView : document.parentWindow;
if ( "Element" in currentWindow ){
Element.prototype.getElementsByClassName = document.getElementsByClassName;
}
else {
var tableElements = [ "table", "thead", "tfoot", "tbody" ];
var create = document.createElement;
document.createElement = function ( tagName ){
var element = create( tagName );
element.getElementsByClassName = document.getElementsByClassName;
var tagName = tagName.toLowerCase();
if ( tagName == "tr" ) redefineInsertProperty( element, "insertCell" );
for ( var x = 0; x < tableElements.length; x++ ){
if ( tagName == tableElements[ x ] ) redefineInsertProperty( element, "insertRow" );
}
return element;
};
document.createElement.toString = function (){ return "function createElement() { [native code] }" };
function redefineInsertProperty ( elem, prop ){
var insert = elem[ prop ];
elem[ prop ] = function ( index ){
//var index = index || 0;
var child = insert( index );
child.getElementsByClassName = document.getElementsByClassName;
if ( child.tagName.toLowerCase() == "tr" ) redefineInsertProperty( child, "insertCell" );
return child;
};
elem[ prop ].toString = function (){ return "function " + prop + "() { [native code] }" };
}
if ( document.addEventListener ){
document.addEventListener( "DOMContentLoaded", function (){
document.removeEventListener( "DOMContentLoaded", arguments.callee, false );
addProperty();
}, false );
}
else if ( document.attachEvent ){
document.attachEvent( "onreadystatechange", function (){
if ( document.readyState === "complete" ){
document.detachEvent( "onreadystatechange", arguments.callee );
addProperty();
}
} );
}
function addProperty (){
var allTags = document.getElementsByTagName( "*" );
var n, x, tag, tagName;
for ( n = 0; n < allTags.length; n++ ){
tag = allTags[ n ];
if ( tag.getElementsByClassName ) continue;
tag.getElementsByClassName = document.getElementsByClassName;
tagName = tag.tagName.toLowerCase();
if ( tagName == "tr" ) redefineInsertProperty( tag, "insertCell" );
for ( x = 0; x < tableElements.length; x++ ){
if ( tagName == tableElements[ x ] ) redefineInsertProperty( tag, "insertRow" );
}
}
}
}
}());
}
'JAVASCRIPT > 그외' 카테고리의 다른 글
| [ 자바스크립트 ] window의 속성 목록 (1) | 2017.04.30 |
|---|---|
| [ 자바스크립트 ] document의 속성 목록 (0) | 2017.04.28 |
| [ 자바스크립트 ] classList (0) | 2016.09.27 |
| [ 자바스크립트 ] offsetParent (0) | 2016.09.19 |
| [ 자바스크립트 ] removeChild() & childNodes() (0) | 2016.03.20 |