跳过For循环中的某些数字

时间:2014-10-24 02:41:32

标签: javascript html

我只是编写一个页面来按实体编号查看每个ASCII条目,我想知道是否有更简单/更清晰的方法来跳过不需要的数字。

<div id="ul-container">
    <ul>
        <script type="text/javascript">
            var x = new Array();
            x[127] = 127;
            x[129] = 129;
            x[141] = 141;
            x[143] = 143;
            x[144] = 144;
            x[157] = 157;
            x[160] = 160;
            x[173] = 173;
            for (i = 0; i <= 200; i++) {
                if (i >= 1 && i <= 32) {}
                else if (i == x[i]) {}
                else {
                    document.write("<li>&#" + i + "<br /><span>&amp;#" + i + "</span></li>");
                }
            }
        </script>
    </ul>
</div>

http://jsfiddle.net/absolutebob/af9xpm5k/

1 个答案:

答案 0 :(得分:2)

我认为通过检查i是否在x中并使用continue关键字来跳过此步骤:

if ((i >= 1 && i <= 32) || x.indexOf(i) > -1) continue;

并将值正常放在x数组中:

var x = [127, 129, 141, 143, 144, 157, 160, 173];

<div id="ul-container">
<ul>
    <script>
        var x = [127, 129, 141, 143, 144, 157, 160, 173];
        for (i = 0; i <= 200; i++) {
        if ((i >= 1 && i <= 32) || x.indexOf(i) > -1) continue;
            document.write("<li>&#" + i + "<br /><span>&amp;#" + i + "</span></li>");
        }
    </script>
</ul>
</div>

<style>
#ul-container {
border-style: none solid;
border-width: 1px;
border-color: #000000;
margin: 0px auto;
width: 400px;
}
#ul-container li {
background-color: #f3f3f3;
display: inline-block;
font-size: 36px;
margin: 12px;
text-align: center;
}
#ul-container li span {
font-size: 16px;
font-weight: normal;
}
</style>