Recently has been gnawing Rhino book, feeling, so wrote a simple JS Dom query
$ = function (val) {
switch (val.charat (0)) {case
' # ': Return
document.getElementById (val.substring (1)); C4/>break;
Case '. ':
val = val.replace ('. ', ');
if (document.getelementsbyclassname) return
Document.getelementsbyclassname (val);
else {
var obj = document.getelementsbytagname (' * '), len = obj.length,arr=[];
for (Var i=0;i<len;i++) {
if (Obj[i].classname = = val) {
arr[arr.length] = obj[i]
;
}
return arr;
}
break;
Default:
if (Document.getelementsbyname (val). length > 0) return
Document.getelementsbyname (val);
else return
document.getElementsByTagName (val);
}
Such an implementation, in the future call ID, just $ (' #idname '), class when $ ('. ClassName '), tagname and name are all made a simple judgment, are directly to pass $ (' name '), I tried, feel OK.
Above this simple package JS Dom Query Instance code is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.