标签:
修改如下
// 注释: 对获取DOM对象方法的封装 var getTag = function ( tag, context, results ) { results = results || []; context = context || document;//防止context是空时报错现象出现 results.push.apply( results, context.getElementsByTagName( tag ) ); return results; }; var getId = function ( id, results ) { results = results || []; results.push( document.getElementById( id ) ); return results; }; var getClass = function ( className, context, results ) { results = results || []; context = context || document; if ( document.getElementsByClassName ) { results.push.apply( results, context.getElementsByClassName( className ) ); } else { each( getTag( ‘*‘, context ), function ( i, v ) { if ( ( ‘ ‘ + v.className + ‘ ‘ ) .indexOf( ‘ ‘ + className + ‘ ‘ ) != -1 ) { results.push( v ); } } ); } return results; }; // 对each方法的封装 var each = function ( arr, fn ) { for ( var i = 0; i < arr.length; i++ ) { if ( fn.call( arr[ i ], i, arr[ i ] ) === false ) { break; } } }; // 通用的get方法 var get = function ( selector, context, results ) { results = results || []; var rquickExpr = /^(?:#([\w-]+)|\.([\w-]+)|([\w]+)|(\*))$/, m = rquickExpr.exec( selector ); if ( m ) { if ( m[ 1 ] ) { results = getId( m[ 1 ], results ); } else if ( m[ 2 ] ) { results = getClass( m[ 2 ], context, results ); } else if ( m[ 3 ] ) { results = getTag( m[ 3 ], context, results ); } else if ( m[ 4 ] ) { results = getTag( m[ 4 ], context, results ); } }
验证以上封装的方法,html如下(为了使用方便,以下封装的js都单独放在common.js文件中,在html中须引入使用)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .rect { width: 400px; height: 50px; margin: 10px 0; } .c { border: 1px dashed red; } .c1 { border: 1px dashed green; } .c2 { border: 1px dashed blue; } </style> <script src="common.js"></script>//把上面的函数单独放在一个js文件中,在html中引入 </head> <body> <div class="c"> <div class="c1 rect"></div> <div class="c2 rect"></div> <div class="c1 rect"></div> <div class="c2 rect"></div> </div> <hr /> <div> <div class="c1 rect"></div> <div class="c2 rect"></div> <div class="c1 rect"></div> <div class="c2 rect"></div> </div> </body> <script type="text/javascript"> get( ‘.c1‘, get( ‘.c‘ )[ 0 ] )[ 1] .style.backgroundColor = ‘lightgreen‘; </script> </html>
// 注释: 对基本DOM数组方法的封装 var getTag = function ( tag, context, results ) { results = results || []; results.push.apply( results, context.getElementsByTagName( tag ) ); return results; }; var getId = function ( id, results ) { results = results || []; results.push( document.getElementById( id ) ); return results; }; var getClass = function ( className, context, results ) { results = results || []; if ( document.getElementsByClassName ) { results.push.apply( results, context.getElementsByClassName( className ) ); } else { each( getTag( ‘*‘, context ), function ( i, v ) { if ( ( ‘ ‘ + v.className + ‘ ‘ ) .indexOf( ‘ ‘ + className + ‘ ‘ ) != -1 ) { results.push( v ); } } ); } return results; }; // 对each方法的封装 var each = function ( arr, fn ) { for ( var i = 0; i < arr.length; i++ ) { if ( fn.call( arr[ i ], i, arr[ i ] ) === false ) { break; } } }; // 通用的方法 var get = function ( selector, context, results ) { results = results || []; context = context || document; // 1 2 3 4 var rquickExpr = /^(?:#([\w-]+)|\.([\w-]+)|([\w]+)|(\*))$/, m = rquickExpr.exec( selector ); if ( m ) { //此处判断context是否为数组,如果不是数组让其成为数组 if ( context.nodeType ) { context = [ context ]; } each( context, function ( i, v ) { if ( m[ 1 ] ) { results = getId( m[ 1 ], results ); } else if ( m[ 2 ] ) { results = getClass( m[ 2 ], this, results );//this可以替代v } else if ( m[ 3 ] ) { results = getTag( m[ 3 ], this, results ); } else if ( m[ 4 ] ) { results = getTag( m[ 4 ], this, results ); } } ); } return results; };
验证以上封装的方法,html如下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .rect { width: 400px; height: 50px; margin: 10px 0; } .c { border: 1px dashed red; } .c1 { border: 1px dashed green; } .c2 { border: 1px dashed blue; } </style> <script src="common.js"></script> </head> <body> <div class="c"> <div class="c1 rect"></div> <div class="c2 rect"></div> <div class="c1 rect"></div> <div class="c2 rect"></div> </div> <hr /> <div class="c"> <div class="c1 rect"></div> <div class="c2 rect"></div> <div class="c1 rect"></div> <div class="c2 rect"></div> </div> </body> <script type="text/javascript"> var l = get( ‘.c‘ ); each( get( ‘.c1‘, l ), function () { this.style.backgroundColor = ‘lightblue‘; } ); </script> </html>
var myPush = function( target, els ) { var j = target.length, i = 0; // Can‘t trust NodeList.length while ( (target[j++] = els[i++]) ) {} target.length = j - 1; }; // 注释: 对基本方法的封装 var getTag = function ( tag, context, results ) { results = results || []; //此处也无须用context=context||[] try { results.push.apply( results, context.getElementsByTagName( tag ) );//ie8及以下版本不支持传入伪数组 } catch ( e ) { myPush( results, context.getElementsByTagName( tag ) );//对ie8及以下版本的支持 } return results; }; var getId = function ( id, results ) { results = results || []; results.push( document.getElementById( id ) ); return results; }; var getClass = function ( className, context, results ) { results = results || []; if ( document.getElementsByClassName ) { results.push.apply( results, context.getElementsByClassName( className ) ); } else { each( getTag( ‘*‘, context ), function ( i, v ) { if ( ( ‘ ‘ + v.className + ‘ ‘ ) .indexOf( ‘ ‘ + className + ‘ ‘ ) != -1 ) { results.push( v ); } } ); } return results; }; // 对each方法的封装 var each = function ( arr, fn ) { for ( var i = 0; i < arr.length; i++ ) { if ( fn.call( arr[ i ], i, arr[ i ] ) === false ) { break; } } }; // 通用的get方法 var get = function ( selector, context, results ) { results = results || []; context = context || document; var rquickExpr = /^(?:#([\w-]+)|\.([\w-]+)|([\w]+)|(\*))$/, m = rquickExpr.exec( selector ); if ( m ) { if ( context.nodeType ) { context = [ context ]; } //此时对context判断是否为字符串,如果是假设已经找出context if ( typeof context == ‘string‘ ) { context = get( context ); } each( context, function ( i, v ) { if ( m[ 1 ] ) { results = getId( m[ 1 ], results ); } else if ( m[ 2 ] ) { results = getClass( m[ 2 ], this, results );//此处可以用this,也可以用v } else if ( m[ 3 ] ) { results = getTag( m[ 3 ], this, results ); } else if ( m[ 4 ] ) { results = getTag( m[ 4 ], this, results ); } } ); } return results; };
验证以上封装的方法,html如下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .rect { width: 400px; height: 50px; margin: 10px 0; } .c { border: 1px dashed red; } .c1 { border: 1px dashed green; } .c2 { border: 1px dashed blue; } </style> <script src="common.js"></script> </head> <body> <div class="c"> <div class="c1 rect"></div> <div class="c2 rect"></div> <div class="c1 rect"></div> <div class="c2 rect"></div> </div> <hr /> <div> <div class="c1 rect"></div> <div class="c2 rect"></div> </div> <hr /> <div class="c"> <div class="c1 rect"></div> </div> </body> <script type="text/javascript"> // 在页面中找到所有的 .c 的节点, 然后在 .c 的后代元素中 // 找符合 .c1 的节点, 并返回 each( get( ‘.c1‘, ‘.c‘ ), function () { this.style.backgroundColor = ‘skyblue‘; } ); </script> </html>
var myPush = function( target, els ) { var j = target.length, i = 0; // Can‘t trust NodeList.length while ( (target[j++] = els[i++]) ) {} target.length = j - 1; }; // 注释: 对基本方法的封装 var getTag = function ( tag, context, results ) { results = results || []; try { results.push.apply( results, context.getElementsByTagName( tag ) ); } catch ( e ) { myPush( results, context.getElementsByTagName( tag ) ); } return results; }; var getId = function ( id, results ) { results = results || []; results.push( document.getElementById( id ) ); return results; }; var getClass = function ( className, context, results ) { results = results || []; if ( document.getElementsByClassName ) { results.push.apply( results, context.getElementsByClassName( className ) ); } else { each( getTag( ‘*‘, context ), function ( i, v ) { if ( ( ‘ ‘ + v.className + ‘ ‘ ) .indexOf( ‘ ‘ + className + ‘ ‘ ) != -1 ) { results.push( v ); } } ); } return results; }; // 对each方法的封装 var each = function ( arr, fn ) { for ( var i = 0; i < arr.length; i++ ) { if ( fn.call( arr[ i ], i, arr[ i ] ) === false ) { break; } } }; //对myTrim方法的封装,去空格 var myTrim = function ( str ) { if ( String.prototype.trim ) { return str.trim(); } else { return str.replace( /^\s+|\s+$/g, ‘‘ ); } }; // 通用的get方法 var get = function ( selector, context, results ) { results = results || []; context = context || document; // 1 2 3 4 var rquickExpr = /^(?:#([\w-]+)|\.([\w-]+)|([\w]+)|(\*))$/, m = rquickExpr.exec( selector ); if ( m ) { if ( context.nodeType ) { context = [ context ]; } // 如果 context 是一个 dom 数组就没有问题了 // 但是 context 是一个选择器字符串. 有可能是 ‘.c‘ // if ( typeof context == ‘string‘ ) { context = get( context ); } each( context, function ( i, v ) { if ( m[ 1 ] ) { results = getId( m[ 1 ], results ); } else if ( m[ 2 ] ) { results = getClass( m[ 2 ], v, results ); } else if ( m[ 3 ] ) { results = getTag( m[ 3 ], this, results ); } else if ( m[ 4 ] ) { results = getTag( m[ 4 ], this, results ); } } ); } return results; }; var select = function ( selector, context, results ) { results = results || []; // 首先处理逗号 var newSelectors = selector.split( ‘,‘ ); // 细节: 一般不考虑两端的空格 each( newSelectors, function ( i, v ) { results.push.apply( results, get( myTrim( v ), context ) ); } ); //此处我们可以用for in方法进行分解 /*for(var k in newSelectors){ var v = newSelectors[k]; v = myTrim(v)//v.trim(); var list = get(v,context); results.push.apply(results,list); }*/ return results; };
验证以上封装的方法,html如下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .rect { width: 400px; height: 50px; margin: 10px 0; } .c1 { border: 1px dashed red; } .c2 { border: 1px dashed green; } .c3 { border: 1px dashed blue; } </style> <script src="common.js"></script> </head> <body> <div class="c"> <div class="c1 rect"></div> <div class="c2 rect"></div> <div class="c3 rect"></div> <div class="c2 rect"></div> </div> <hr /> <div> <div class="c1 rect"></div> <div class="c3 rect"></div> </div> <hr /> <div class="c"> <div class="c1 rect"></div> </div> </body> <script> var list = select( ‘.c1, .c2‘ ); each( list, function () { this.style.backgroundColor = ‘skyblue‘; }); </script> </html>
var myPush = function( target, els ) { var j = target.length, i = 0; // Can‘t trust NodeList.length while ( (target[j++] = els[i++]) ) {} target.length = j - 1; }; // 注释: 对基本方法的封装 var getTag = function ( tag, context, results ) { results = results || []; try { results.push.apply( results, context.getElementsByTagName( tag ) ); } catch ( e ) { myPush( results, context.getElementsByTagName( tag ) ); } return results; }; var getId = function ( id, results ) { results = results || []; results.push( document.getElementById( id ) ); return results; }; var getClass = function ( className, context, results ) { results = results || []; if ( document.getElementsByClassName ) { results.push.apply( results, context.getElementsByClassName( className ) ); } else { each( getTag( ‘*‘, context ), function ( i, v ) { if ( ( ‘ ‘ + v.className + ‘ ‘ ) .indexOf( ‘ ‘ + className + ‘ ‘ ) != -1 ) { results.push( v ); } } ); } return results; }; // 对each方法的封装 var each = function ( arr, fn ) { for ( var i = 0; i < arr.length; i++ ) { if ( fn.call( arr[ i ], i, arr[ i ] ) === false ) { break; } } }; //对myTrim方法的封装,去空格 var myTrim = function ( str ) { if ( String.prototype.trim ) { return str.trim(); } else { return str.replace( /^\s+|\s+$/g, ‘‘ ); } }; // 通用的get方法 var get = function ( selector, context, results ) { results = results || []; context = context || document; // 1 2 3 4 var rquickExpr = /^(?:#([\w-]+)|\.([\w-]+)|([\w]+)|(\*))$/, m = rquickExpr.exec( selector ); if ( m ) { if ( context.nodeType ) { context = [ context ]; } // 如果 context 是一个 dom 数组就没有问题了 // 但是 context 是一个选择器字符串. 有可能是 ‘.c‘ // if ( typeof context == ‘string‘ ) { context = get( context ); } each( context, function ( i, v ) { if ( m[ 1 ] ) { results = getId( m[ 1 ], results ); } else if ( m[ 2 ] ) { results = getClass( m[ 2 ], v, results ); } else if ( m[ 3 ] ) { results = getTag( m[ 3 ], this, results ); } else if ( m[ 4 ] ) { results = getTag( m[ 4 ], this, results ); } } ); } return results; }; var select = function ( selector, context, results ) { results = results || []; // 首先处理逗号 var newSelectors = selector.split( ‘,‘ ); // 细节: 一般不考虑两端的空格 each( newSelectors, function ( i, v ) { var list = v.split(‘ ‘); var cont = context; for(var i = 0;i <list.length;i++){ //如果list[i]是空字符串,会生成一个新数组,则需要进行如下判断 if(list[i] === ‘‘) continue; cont = get(list[i],cont); } results.push.apply( results, cont ); } ); return results; };
验证以上封装的方法,html如下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .rect { width: 400px; height: 50px; margin: 10px 0; } .c1 { border: 1px dashed red; } .c2 { border: 1px dashed green; } .c3 { border: 1px dashed blue; } </style> <script src="common.js"></script> </head> <body> <div class="c"> <div class="c1 rect"></div> <div class="c2 rect"></div> <div class="c3 rect"><p>123</p></div> <div class="c2 rect"></div> </div> <hr /> <div> <div class="c1 rect"></div> <div class="c3 rect"></div> </div> <hr /> <div class="c"> <div class="c1 rect"></div> </div> </body> <script> var list = select( ‘.c .c2‘ ); each( list, function () { this.style.backgroundColor = ‘skyblue‘; }); </script> </html>
标签:
原文地址:http://www.cnblogs.com/goweb/p/5380877.html