给你一个想法:
现场演示
(function(window){
var myLibrary = (function( s ) {
var d = document,
e = d.getElementById( s ),
methods = {
one : function(val){
alert(val);
return this; // maintain chainability
},
two : function(val){
alert(val);
return this; // maintain chainability
},
css : function( property, val){
if(!val && typeof property == "object" ){ // styles in Object notation
for(var key in property){
e.style[key] = property[key];
}
}else{ // Comma separated: property, value
e.style[property] = val || undefined;
}
return this;
}
};
return methods;
});
window.myLibrary = window.de = myLibrary; // Make Window accept "de" as "myLib" alias.
})(window);
de("Id").one("UGA!!!").two("yoo").css({color:"red", background:"gold"});
考虑到你有类似的东西:
<div id="Id">Some element</div>