需要帮助使用innerHTML和数组输入变量 [英] Need helping with innerHTML and array input variables

查看:86
本文介绍了需要帮助使用innerHTML和数组输入变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这个来接受用户输入。取该值并减去一个,以便可以调用和打印5个数组中的一个。如果选择5,则打印所有5个阵列;

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML1 .0Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title> Test2< / title>
< / head>
< body>
< input type =buttononclick =dutyarray()value =Click>查看我的顶部< input type =numberid =counter>工作职责< /输入>
< br />
< p id =任务>职责< / p>
< script>
函数dutyarray()
{
var x = document.getElementById('counter')。value;
x = parseInt(x);

var duty = new Array();
税[0] =操作设备;
值[1] =检查水损坏;
duty [2] =检查模具;
税[3] =遵照保险程序;
税[4] =恢复; (x> = 0; x-1)
{
document.getElementById('task')。innerHTML = duty [x] +< br>;
}
}
< / script>


解决方案

您可以使用

  var duty = [
操作设备,
检查水损坏,
检查模具,
Follow Insurance Procedure,
Restore
],
x = parseInt(document.getElementById('counter')。value,10);
document.getElementById('task')。innerHTML = duty.slice(0,x).join('< br />');






注意 p>


  • 使用 parseInt 或<$时,始终使用第二个参数(基数) c $ c> parseFloat 。


  • 如果 document.getElementById('counter' code>是一个数字字符串(如'3'),不要使用 parseInt 。相反,如果需要,可以使用 +'3'来获取数字 3



    如果您想将'3px'>分析为<$ c>,请仅使用 parseInt 不要使用 getElementById 来获得。$ c $> $ <$ code>。



  • 然后,而不是

      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

    然后,而不是

    <$>在循环的每次迭代中,对于(var i = 0; i< n; ++ i){
    element.innerHTML + = someHTML(i); pre $ // n DOM修改
    }

    使用

      var html =''; $ i $ b $ for(var i = 0; i  html + = someHTML(i); 
    }
    element.innerHTML = html; //只有1个DOM修改



I need this to take user input. Take the value and subtract one so that one of the 5 arrays can be called and printed. If 5 is chosen, all 5 arrays are printed; if 3 is chosen arrays 0 through 2 are printed.

<!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>

解决方案

You could use

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 />');


Notes

  • Always use the second argument (radix) when using parseInt or parseFloat.

  • If document.getElementById('counter').value is a numerical string (like '3'), don't use parseInt. Instead, if necessary, use +'3' to get the number 3.

    Only use parseInt if you want to parse something like '3px' into 3.

  • Don't use getElementById to get the same element at each iteration of a loop.

    Then, instead of

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

    use

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

  • Never change innerHTML at each iteration of a loop, because modifying the DOM has bad performance.

    Then, instead of

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

    use

    var html = '';
    for(var i = 0; i < n; ++i) {
       html += someHTML(i);
    }
    element.innerHTML = html; // only 1 DOM modification
    

这篇关于需要帮助使用innerHTML和数组输入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆