如何使用二维数组和一个for循环优化我的代码?

时间:2017-07-21 13:27:28

标签: javascript html css

我试图通过仅使用一个for循环,一个二维数组或动态创建s来优化我的代码,但十进制字符应该像现在一样是静态的,因为我随机插入空格(& nbsp)在人物之间。请帮我优化代码。

以下是代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>

        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>Test fonts</title>
        <style>
            @font-face{
                font-family: 'test';
                src: url('ArialMonospacedMTPro.otf');
            }

            .testCode_num {
                font-family: 'test' !important;
                height: auto;
                font-size:26px;
                display: inline-block;
            }
            .mainDiv {
                border:1px solid red;
                display: inline-block;
            }

            span{
                color: #5DADE2;
            }

            body {
                background-color: white;
                padding-left: 70px;
            }
        </style>
    </head>

    <body>

        <h1>Character Alignment for <span>Arial Mono MT Pro</span></h1>

        <div id="checkChar_upperCase1" class = 'mainDiv'></div></br>
        <div id="checkChar_upperCase2" class = 'mainDiv'></div></br>
        <div id="checkChar_lowerCase1" class = 'mainDiv'></div></br>
        <div id="checkChar_lowerCase2" class = 'mainDiv'></div></br>
        <div id="checkChar_num" class = 'mainDiv'></div></br>
        <div id="checkChar_spChar" class = 'mainDiv'></div></br>
        <h4>French characters</h4>
        <div id="checkChar_frenChar1" class = 'mainDiv'></div></br>
        <div id="checkChar_frenChar2" class = 'mainDiv'></div></br>

        <script>
            var charDiv_upperCase1 = document.getElementById("checkChar_upperCase1");
            var charDiv_upperCase2 = document.getElementById("checkChar_upperCase2");
            var charDiv_lowerCase1 = document.getElementById("checkChar_lowerCase1");
            var charDiv_lowerCase2 = document.getElementById("checkChar_lowerCase2");
            var charDiv_num = document.getElementById("checkChar_num");
            var charDiv_spChar = document.getElementById("checkChar_spChar");
            var checkChar_frenChar1 = document.getElementById("checkChar_frenChar1");
            var checkChar_frenChar2 = document.getElementById("checkChar_frenChar2");

            /* Upper case  */
            var s_char_upper1 = ['&#160;','&#065;', '&#160;','&#066;','&#067;','&#068;', '&#160;','&#069;','&#070;',                        '&#160;','&#071;','&#072;', '&#160;','&#073;','&#074;', '&#160;','&#075;','&#076;','&#077;', '&#160;'];
            for(var i = 0; i < s_char_upper1.length; i++) {
                var ele = document.createElement("div");
                ele.className = "testCode_num";
                ele.innerHTML = s_char_upper1[i];       
                charDiv_upperCase1.appendChild(ele);
                var style = getComputedStyle(ele, null);
                var charWidth = parseInt(style.width);
                console.log("Upper case Row1 i: "+i+"  value :  " + ele.innerHTML + " width of char : "+ charWidth);
            }

            var s_char_upper2 = ['&#160;', '&#160;','&#078;', '&#079;','&#080;', '&#160;','&#081;', '&#082;','&#083;','&#084;', '&#160;',                   '&#085;', '&#160;','&#086;','&#087;', '&#160;','&#088;','&#089;', '&#160;','&#090;'];
            for(var i = 0; i < s_char_upper2.length; i++) {
                var ele = document.createElement("div");
                ele.className = "testCode_num";
                ele.innerHTML = s_char_upper2[i];
                charDiv_upperCase2.appendChild(ele);
                var style = getComputedStyle(ele, null);
                var charWidth = parseInt(style.width);
                console.log("Upper case Row2 i: "+i+"  value :  " + ele.innerHTML + " width of char : "+ charWidth);
            }

            /* Lower Case */
            var s_char_lower1 = ['&#160;','&#097;', '&#160;','&#098;','&#099;','&#100;', '&#160;','&#101;','&#102;',                        '&#160;','&#103;','&#104;', '&#160;','&#105;','&#106;', '&#160;','&#107;','&#108;','&#109;', '&#160;'];
            for(var i = 0; i < s_char_lower1.length; i++) {
                var ele = document.createElement("div");
                ele.className = "testCode_num";
                ele.innerHTML = s_char_lower1[i];
                charDiv_lowerCase1.appendChild(ele);
                var style = getComputedStyle(ele, null);
                var charWidth = parseInt(style.width);
                console.log("Lower Case row1 i: "+i+"  value :  " + ele.innerHTML + " width of char : "+ charWidth);
            }

            var s_char_lower2 = ['&#160;', '&#160;', '&#110;','&#111;', '&#160;','&#112;', '&#113;','&#114;', '&#115;', '&#160;',                   '&#116;','&#117;', '&#160;','&#118;','&#119;', '&#160;','&#120;', '&#121;', '&#160;', '&#122;'];
            for(var i = 0; i < s_char_lower2.length; i++) {
                var ele = document.createElement("div");
                ele.className = "testCode_num";
                charDiv_lowerCase2.appendChild(ele);
                ele.innerHTML = s_char_lower2[i];
                var style = getComputedStyle(ele, null);
                var charWidth = parseInt(style.width);
                console.log("Lower Case row2 i: "+i+"  value :  " + ele.innerHTML + " width of char : "+ charWidth);
            }

            /* Numbers */
            var s_char_num = ['&#160;', '&#048;', '&#160;','&#049;','&#050;', '&#160;','&#051;','&#052;','&#053;', '&#160;',                    '&#160;','&#054;','&#055;', '&#160;','&#056;','&#057;','&#048;','&#049;', '&#160;','&#050;'];
            for(var i = 0; i < s_char_num.length; i++) {
                var ele = document.createElement("div");
                ele.className = "testCode_num";
                ele.innerHTML = s_char_num[i];
                charDiv_num.appendChild(ele);
                var style = getComputedStyle(ele, null);
                var charWidth = parseInt(style.width);
                console.log("Num Row i: "+i+"  value :  " + ele.innerHTML + " width of char : "+ charWidth);
            }

            /*special characters 1 */
            var s_char_spec = ['&#160;', '&#033;', '&#160;','&#034;','&#035;', '&#160;','&#036;','&#037;','&#038;', '&#139;',                   '&#160;','&#040;','&#041;', '&#160;','&#042;','&#043;','&#044;','&#045;', '&#160;','&#046;'];
            for(var i = 0; i < s_char_spec.length; i++) {
                var ele = document.createElement("div");
                ele.className = "testCode_num";
                ele.innerHTML = s_char_spec[i];
                charDiv_spChar.appendChild(ele);
                var style = getComputedStyle(ele, null);
                var charWidth = parseInt(style.width);
                console.log("special chars Row1 i: "+i+"  value :  " + ele.innerHTML + " width of char : "+ charWidth);
            }

            var frenchar_Upper = ['&#160;', '&#192;', '&#160;', '&#194;', '&#198;', '&#160;', '&#199;', '&#202;', '&#160;', '&#203;',                   '&#206;', '&#160;', '&#207;', '&#212;', '&#140;', '&#219;', '&#160;', '&#128;', '&#8355;', '&#160;'];
            for(var i = 0; i < frenchar_Upper.length; i++) {
                var ele = document.createElement("div");
                ele.className = "testCode_num";
                ele.innerHTML = frenchar_Upper[i];
                checkChar_frenChar1.appendChild(ele);
                var style = getComputedStyle(ele, null);
                var charWidth = parseInt(style.width);
                console.log("frenchar_Upper i: "+i+"  value :  " + ele.innerHTML + " width of char : "+ charWidth);
            }

            var frenchar_Lower = ['&#160;', '&#224;', '&#160;', '&#226;', '&#160;', '&#230;', '&#160;', '&#231;', '&#234;', '&#160;',                   '&#235;', '&#160;', '&#238;', '&#239;','&#160;', '&#244;', '&#156;','&#252;','&#171;','&#187;'];
            for(var i = 0; i < frenchar_Lower.length; i++) {
                var ele = document.createElement("div");
                ele.className = "testCode_num";
                ele.innerHTML = frenchar_Lower[i];
                checkChar_frenChar2.appendChild(ele);
                var style = getComputedStyle(ele, null);
                var charWidth = parseInt(style.width);
                console.log("frenchar_Lower i: "+i+"  value :  " + ele.innerHTML + " width of char : "+ charWidth);
            }

        </script>
    </body>
</html>

0 个答案:

没有答案