html 요소의 현재 배경색 가져오기

1642 단어
/**
     *  
     */
    function getBg(element) {//author: Longbill (www.longbill.cn)  
        //dnew.cn   
        var rgbToHex = function(rgbarray, array) {
            if (rgbarray.length < 3)
                return false;
            if (rgbarray.length == 4 && rgbarray[3] == 0 && !array)
                return 'transparent';
            var hex = [];
            for ( var i = 0; i < 3; i++) {
                var bit = (rgbarray[i] - 0).toString(16);
                hex.push((bit.length == 1) ? '0' + bit : bit);
            }
            return array ? hex : '#' + hex.join('');
        }
        //---------------  
        if (typeof element == "string")
            element = document.getElementById(element);
        if (!element)
            return;
        cssProperty = "backgroundColor";
        mozillaEquivalentCSS = "background-color";
        if (element.currentStyle)
            var actualColor = element.currentStyle[cssProperty];
        else {
            var cs = document.defaultView.getComputedStyle(element, null);
            var actualColor = cs.getPropertyValue(mozillaEquivalentCSS).match(
                    /\d{1,3}/g);
            //-----  
            actualColor = (actualColor) ? rgbToHex(actualColor) : "transparent";
        }
        if (actualColor == "transparent" && element.parentNode)
            return arguments.callee(element.parentNode);
        if (actualColor == null)
            return "#ffffff";
        else
            return actualColor;
    }

좋은 웹페이지 즐겨찾기