使用Google闭包,通过类和id选择器获取DOM元素
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Google闭包,通过类和id选择器获取DOM元素相关的知识,希望对你有一定的参考价值。
The Google Closure lets you get DOM elements with getElement( id ) and getElementByClass( class ). This function combines the two to let you get elements similarly to the way jQuery does. E.g. getElement( '#home .sidebar #links') returns the element with an id of 'links', within the element of class 'sidebar', within the element of id 'home'.
function getElement( selector ) { var split = selector.split(" "); var el = document; var len = split.length; var type, name; for( var i = 0; i < len; i++ ) { try { name = split[ i ]; type = name[ 0 ]; name = name.substr( 1, name.length - 1 ); switch( type ) { case "#": el = goog.dom.getElement( name, el ); break; case ".": el = goog.dom.getElementByClass( name, el ); break; default: el = null; } } catch( e ) { el = null; throw( "Error with getElement using selector: " + selector + ", error: " + e ); } } if ( el == document ) el = null; return el; }
以上是关于使用Google闭包,通过类和id选择器获取DOM元素的主要内容,如果未能解决你的问题,请参考以下文章