简体   繁体   English

如何循环遍历 html 中的表格并使用 javascript 迭代它们的值

[英]how to loop over a table in html and iterate their values using javascript

I'm building a grid (20 rows and 20 columns ( of squares) ) but struggling with looping on table values to actually make the grid我正在构建一个网格(20 行和 20 列(正方形))但正在努力循环表值以实际制作网格

I provided more info on the html code..我提供了有关 html 代码的更多信息..

The pictures are provided on the comments图片在评论中提供

    <body>
        <h1>Pixel Art Maker</h1>

    <h2>Choose Grid Size</h2>
        <form id="sizePicker">
            Grid Height:  <!--- Creating input element to input height --->
            <input type="number" id="inputHeight" name="height" min="1" value="1"> 
            Grid Width: <!--- Creating input element to input width --->
            <input type="number" id="inputWidth" name="width" min="1" value="1">
            <input type="submit">
        </form>

    <h2>Pick A Color</h2>
    <input type="color" id="colorPicker"> <!--- Creating imput element to input a certain color --->

    <h2>Design Canvas</h2>
    <table id="pixelCanvas"> <tr> <td>  </td>  </tr> </table>
    <!--- Creating the table --->

    <script src="designs.js"></script>
    </body>

    table,
    tr,
    td {
        border: 1px solid black;
    }
    tr {
        height: 20px; //some how this is working as it's the same as 
                      //td when i edit it the td gets edited aswell
        width: 20px;
    }

    td {
        height: 20px; //same as here
        width: 20px;
    }

    const lTable = document.querySelector("pixelCanvas"); 
    //Selecting my table and storing it in a variable

    let height = document.querySelector("#inputHeight");
    //Storing the value on the input height element
    let width = document.querySelector("#inputWidth");
    //Storing the value of the input width element

    function makeGrid() {

    //This is the main problem i can't figure out how to iterate the value
    // to make the actual grid and give it the event listener

}

you do not need a form for this unless you want to submit data to a API.除非您想向 API 提交数据,否则您不需要为此提供表单。

find a minimal solution for your problem here:在这里为您的问题找到一个最小的解决方案:

 function makeGrid() { let height = document.querySelector("#inputHeight").value; //Storing the value on the input height element let width = document.querySelector("#inputWidth").value; //Storing the value of the input width element //Selecting my table and storing it in a variable const lTable = document.querySelector("#pixelCanvas"); //This is the main problem i can't figure out how to iterate the value // to make the actual grid and give it the event listener for (var i=0; i< height;i++) { var row = lTable.insertRow(i); for (var j=0; j< width;j++) { row.insertCell(j); } } }
 table { border: 1px solid black; } table td { border: 1px solid black; height: 20px; width: 20px; }
 <div> <label for="inputHeight">Height</label> <input type="text" id="inputHeight" value="0"/> </div> <div> <label for="inputWidth">Width</label> <input type="text" id="inputWidth" value="0"/> </div> <input type="submit" value="Draw" onClick="makeGrid()"></input> <table id="pixelCanvas"></table>

A fiddle that may help you:一个可以帮助你的小提琴:

https://jsfiddle.net/qh6zjfmw/ https://jsfiddle.net/qh6zjfmw/

Here the function, every cell have data-row and data-col that help you to understand the clicked element, and every cell have .row-number and .col-number to target the correct element to change the class (active/inactive)这里的函数,每个单元格都有 data-row 和 data-col 来帮助你理解点击的元素,每个单元格都有 .row-number 和 .col-number 来定位正确的元素来改变类(活动/非活动)

    makegrid(13,13);

    function makegrid(rows, cols){
        for (var r = 0; r < rows; r++){
        console.log(r+1);
        $('<div class="row row-'+r+'"></div>').appendTo(".grid");

          for (var c = 0; c < cols; c++){
            console.log(c+1);
            $('<div class="col col-'+c+'" style="width:'+100/cols+'%; padding-bottom:'+100/cols+'%" data-row="'+r+'"  data-col="'+c+'"></div>').appendTo(".grid .row-"+r);

          }

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何循环字符串数据类型并使用 javascript 对其进行迭代以将数据显示为 html 中的列表项? - How to loop over string data type & iterate over it using javascript for displaying the data as list item in html? 如何循环和遍历包含objetcs和交换值的Javascript对象 - How to loop and iterate over Javascript objects containing objetcs and swap values 如何遍历HTML表列并使用JQuery将值输入到Javascript数组中 - How to iterate through an HTML table column and input the values into a Javascript array using JQuery 使用 javascript 遍历值列表 - Iterate over a list of values using javascript 如何遍历 HTML 元素并使用 for 循环对值求和 - how to iterate through HTML elements and sum the values using for loop 如何迭代/循环包含 HTML 标记的文本字符串,同时忽略 Javascript 中的那些 HTML 标记? - How to iterate/loop over a text string that contains HTML tags while ignoring those HTML tags in Javascript? 如何使用javascript遍历html表输入 - How to iterate through a html table inputs using javascript 如何使用 javascript 遍历 html? - How to loop over an html with javascript? For 循环 (JavaScript).. 迭代绑定中保存的值..? - For loop (JavaScript).. iterate over values held in bindings..? 遍历数组并将值分配给表中的列-javascript - iterate over array and assign values to columns in table - javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM