0

我需要这个来接受用户输入。取值并减去 1,以便可以调用和打印 5 个数组之一。如果选择 5,则打印所有 5 个数组;如果选择 3,则打印数组 0 到 2。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test2</title>
</head>
<body>
<input type="button" onclick="dutyarray()" value="Click">To see my top<input type="number"     id="counter"> job duties here</input>
<br />
<p id="task">Duties</p>
<script>
function dutyarray()
{
    var x = document.getElementById('counter').value;
    x = parseInt(x);

    var duty= new Array();
    duty[0] = "Operate Equipment";
    duty[1] = "Check for Water Damage";
    duty[2] = "Check for Mold";
    duty[3] = "Follow Insurance Procedure";
    duty[4] = "Restore";        

    while(x >= 0; x - 1)
    {
        document.getElementById('task').innerHTML = duty[x] + <br>;
    }
}
</script>
4

2 回答 2

2

你可以使用

var duty = [
        "Operate Equipment",
        "Check for Water Damage",
        "Check for Mold",
        "Follow Insurance Procedure",
        "Restore"
    ],  
    x = parseInt( document.getElementById('counter').value, 10 );
document.getElementById('task').innerHTML = duty.slice(0, x).join('<br />');

笔记

  • parseInt使用or时始终使用第二个参数(基数)parseFloat

  • 如果document.getElementById('counter').value是数字字符串(如'3'),请不要使用parseInt. 相反,如有必要,使用+'3'来获取号码3

    parseInt在您想解析类似'3px'into的内容时使用3

  • 不要用于getElementById在循环的每次迭代中获取相同的元素。

    然后,而不是

    for(var i = 0; i < n; ++i) {
       doSomething(document.getElementById('foo'), i);
    }
    

    利用

    var el = document.getElementById('foo');
    for(var i = 0; i < n; ++i) {
       doSomething(el, i);
    }
    
  • 永远不要innerHTML在循环的每次迭代中更改,因为修改 DOM 会降低性能。

    然后,而不是

    for(var i = 0; i < n; ++i) {
       element.innerHTML += someHTML(i); // n DOM modifications
    }
    

    利用

    var html = '';
    for(var i = 0; i < n; ++i) {
       html += someHTML(i);
    }
    element.innerHTML = html; // only 1 DOM modification
    
于 2013-11-01T02:01:52.050 回答
0
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Test2</title>
</head>
<body>
    <input type="button" onclick="dutyarray()" value="Click">To see my top
    <input type="number" id="counter"> job duties here</input>
    <br />
    <p id="task">Duties</p>
    <script>
        function dutyarray() {
            var x = document.getElementById('counter').value;
            x = parseInt(x);

            var duty =
                [
                "Operate Equipment",
                "Check for Water Damage",
                "Check for Mold",
                "Follow Insurance Procedure",
                "Restore"
                ];

            if (x > duty.length-1) x = duty.length-1;   // Performs some checking

            document.getElementById('task').innerHTML = "";
            for ( ; x >= 0; x--) {
                document.getElementById('task').innerHTML += duty[x] + "<br>";
            }
        }
    </script>
</body>
</html>
于 2013-11-01T02:03:11.657 回答