Skip to content

Instantly share code, notes, and snippets.

@5509
Created April 7, 2011 02:44
Show Gist options
  • Save 5509/906930 to your computer and use it in GitHub Desktop.
Save 5509/906930 to your computer and use it in GitHub Desktop.
jQueryのCSSみたいなやつの簡易(しょぼい)版
/*
* CSS - set css style(s)
*
* HOW TO USE
* set css style : elm.css(type, val)
* set css styles : elm.css({type1: val1, type2: val2, ...})
*/
Element.prototype.css = function() { // arguments: (style, val) or {}
var _a = arguments, i, hash;
// 一括指定
if ( typeof _a[0] === "object" ) {
hash = _a[0];
for ( i in hash ) {
this.style[i] = setPx(i, hash[i]);
}
// 単体指定
} else {
this.style[_a[0]] = setPx(_a[0], _a[1]);
}
// pxが抜けていてかつpxが必要なtypeにはpxを付加して返す
function setPx(type, oldVal) {
var newVal = "undefined";
if ( type.indexOf(".") === -1
&& !/opacity|zIndex|zoom/.test(type)
&& /^\d*$/.test(oldVal) ) {
newVal = oldVal + "px";
} else {
newVal = oldVal;
}
return newVal;
}
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment