辨色器

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>Color Detector</title>  
<script type="text/javascript">  
    function calc() {  
        var color = document.getElementById("color").value;  
        if(color.length!=7 && color.length!=9) {  
            alert(color.length + "The format of color is not correct!\r\n example: #010203 or #AA010203");  
            return;  
        }  
        var opacity = 1;  
        if(color.length==9) {  
            opacity = "0x" + color.substring(1, 3);  
            opacity = parseInt(opacity)/255;  
            color = '#' + color.substring(3, color.length);  
              
        }  
          
        var boxstyle = document.getElementById('box').style;  
        boxstyle.background=color;  
        boxstyle.opacity =opacity;  
    }  
</script>  
</head>  
<body>  
    <div id="box" style="width: 150px; height: 50px; background: #e6e6e6; opacity:1"></div>  
    <input type="text" id="color" name="color" value="#e6e6e6" /> <input type="button" value="Ok" onclick="javascript: calc();"/>  
</body>  
</html> 

 

posted @ 2020-11-16 18:02  xh_Blog  阅读(160)  评论(0编辑  收藏  举报