美文网首页
JS案例6-用for循环为文本框赋值和取值

JS案例6-用for循环为文本框赋值和取值

作者: hi__world | 来源:发表于2018-10-17 09:18 被阅读0次

效果演示:


效果演示

源码:

<!DOCTYPE html>
<html>
<head>
    <title>demo</title>
</head>
<style>
    input{width: 20px}
</style>
<body>
    <!-- 标签 -->
    <input type="text">
    <input type="text">
    <input type="text">
    <input type="text">
    <input type="text">
    <button>让其值从0开始</button>
    <button>让其值赋给一个数组在控制台打印</button>
    <!-- JS -->
<script>
    var inp=document.getElementsByTagName("input");
    var btn=document.getElementsByTagName("button");

    btn[0].onclick=function(){
        for(var i=0;i<inp.length;i++){
            inp[i].value=i;
        }
    }

    btn[1].onclick=function(){
        var newArr=[];
        for(var i=0;i<inp.length;i++){
            newArr.push(inp[i].value);
        }
        console.log(newArr)
    }
</script>
</body>
</html>

相关文章

网友评论

      本文标题:JS案例6-用for循环为文本框赋值和取值

      本文链接:https://www.haomeiwen.com/subject/txkkzftx.html