getstyle
var getStyle = function (elem, style) { return 'getComputedStyle' in window ? getComputedStyle(elem, null)[style] : function () { style = style.replace(/-(w)/g, function ($, $1) { return $1.toUpperCase(); }); var val = elem.currentStyle[style]; if (val === 'auto' && (style === "width" || style === "height")) { var rect = elem.getBoundingClientRect(); if (style === "width") { return rect.right - rect.left + 'px'; } else { return rect.bottom - rect.top + 'px'; } } return val; }(); }; // 调用该方法 var test = document.getElementById('test'), // 获取计算的宽度 tWidth = getStyle(test, 'width');
class操作
function hasClass(obj, cls) {
//热皮球 return obj.className.match(new RegExp('(\s|^)' + cls + '(\s|$)')); } function addClass(obj, cls) { if (!this.hasClass(obj, cls)) obj.className += " " + cls; } function removeClass(obj, cls) { if (hasClass(obj, cls)) { var reg = new RegExp('(\s|^)' + cls + '(\s|$)'); obj.className = obj.className.replace(reg, ' '); } } function toggleClass(obj, cls) { if (hasClass(obj, cls)) { removeClass(obj, cls); } else { addClass(obj, cls); } } function toggleClassTest() { var obj = document.getElementById('test'); toggleClass(obj, "testClass"); }