zoukankan      html  css  js  c++  java
  • 各种常见兼容代码

    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"); }
  • 相关阅读:
    POJ 1315 Don't Get Rooked
    POJ 2051 Argus
    POJ 1942 Paths on a Grid
    POJ 2151 Check the difficulty of problems
    POJ 3349 Snowflake Snow Snowflakes
    POJ 1753 Flip Game
    POJ 2392 Space Elevator
    POJ 2385 Apple Catching
    POJ 2356 Find a multiple
    POJ 2355 Railway tickets
  • 原文地址:https://www.cnblogs.com/dhsz/p/8458890.html
Copyright © 2011-2022 走看看