var setCSS = function(_this,cssOption){ if(!_this || _this.nodeType === 3 || _this.nodeType === 8 || !_this.style){ return; } for(var cs in cssOption){ _this.style[cs] = cssOption[cs]; } return _this; };
使用时
setCSS(xxx,{ "position":"relative", "top":"0px" });
7 自适应文本框
采用scrollHeight复制给style.height
xxx.style.overflowY = "hidden"; xxx.onkeyup = function(){ xxx.style.height = xxx.scrollHeight+"px"; };
8 复选框全选、取消和反选
//下面html代码 <label> <input type="checkbox">读书 </label> <label> <input type="checkbox">跑步 </label> <label> <input type="checkbox">游戏 </label> <label> <input type="checkbox">游泳 </label> //下面是js代码 var targets = document.getElementsByName("actionSelects"); var targetsLen = targets.length; var i = 0; document.getElementById("allSelect").onclick = function(){ for(i=0;i<targetsLen;i++){ targets[i].checked = true; } } document.getElementById("cancelAllSelect").onclick = function(){ for(i=0;i<targetsLen;i++){ targets[i].checked = false; } } document.getElementById("_select").onclick = function(){ for(i=0;i<targetsLen;i++){ targets[i].checked = !targets[i].checked; } }