简体   繁体   English

使用jQuery和JavaScript在HTML表格中创建行和单元格

[英]Using jQuery and JavaScript to create rows and cells in an HTML table

I've been browsing around google and stack overflow to find an answer, but I'd like to get a direct answer to my question, thus the post. 我一直在谷歌周围浏览和堆栈溢出找到答案,但是我想直接回答我的问题,因此发布。

So, I'm very new to JavaScript and jQuery, so pardon any ridiculousness haha. 所以,我对JavaScript和jQuery还是很陌生,所以请原谅任何荒谬的哈哈。

$(document).ready(function(){

  getGridSize();

});


function getGridSize() {
  sizeWidth = parseInt(prompt("Width?"));
  sizeHeight = parseInt(prompt("Height?"));
  fillGrid(sizeWidth, sizeHeight);
}

function fillGrid(width, height) {
  for (i = 0; i < height; i++) {
    $('table').append("<tr></tr>");
    for (j = 0; j < width; j++) {
      $('tr').append("<td><div></div></td>")
    };
  };
}

The above is what I have in my .js file currently. 以上是我目前在.js文件中所拥有的。 I'm using a skeleton html file with an empty <table> </table> in the body. 我使用的是一个html框架文件,其中的主体中有一个空的<table> </table>

What I'm trying to do is create a "width" and "height" of a table and fill in <div> in each cell. 我想做的是创建一个表的“宽度”和“高度”,并在每个单元格中填写<div> I have a css file to change the <div> files into small squares with black borders. 我有一个CSS文件,可将<div>文件更改为带有黑色边框的小方块。

I saw a tutorial recently by Helping Develop where they create a slider script with jQuery and JS. 我最近在Helping Develop上看到了一个教程,其中他们使用jQuery和JS创建滑块脚本。 The code really reminded me of a class element in Ruby using the $(document).ready(function() block as an initialize. 该代码确实使我想起了Ruby中使用$(document).ready(function()块作为初始化的类元素。

My initial attempt at this project is an emulation of what I saw and what I've experienced with Ruby. 我对该项目的最初尝试是对我所看到的以及我对Ruby所经历的模仿。 However, it didn't turn out as expected and I'd like to get some feedback on what I've written so far. 但是,结果没有达到预期,我想就到目前为止所写的内容获得一些反馈。

Anyways, all constructive criticism welcome. 无论如何,欢迎所有建设性的批评。 Is it ok to think of the jQuery ready block as a similar function to an initialize method in Ruby? 是否可以将jQuery ready块视为与Ruby中的initialize方法类似的函数? Any no-no's you see here that I should avoid in the future? 您在这里看到的任何禁忌,我以后都应该避免吗? Any helpful nudges in the right direction please? 请在正确的方向上提供帮助吗? :) :)

Edit: I think I've caught an error with my and creations. 编辑:我想我和我的创作发现了一个错误。 Will edit once testing it out. 测试完成后将进行编辑。

Edit2: Ok, my other attempt didn't work out. Edit2:好的,我的其他尝试没有成功。 :( still seeking help :(仍在寻求帮助

you can do this with 你可以做到这一点

function fillGrid(width, height) {
    var htm = "";
  for (i = 0; i < height; i++) {
      htm += "<tr>";

      for (j = 0; j < width; j++) {
          htm += "<td></td>"
      };
      htm += "</tr>"
  };
    $("table").html(htm);
}

But please don't, this is a terrible practice and there are much easier ways to handle dynamic content in pages, I'd highly recommend you to learn knockout.js if you want to handle this type of thing properly, and I find it's tutorial to be particularly good compared to most other tools so it's not too much of a hassle to go through it 但是请不要这样,这是一种糟糕的做法,并且有很多更简单的方法来处理页面中的动态内容,如果您想正确处理这种类型的事情,我强烈建议您学习kickout.js ,我发现它是与大多数其他工具相比,本教程特别出色,因此无需过多麻烦

You should change the function fillGrid to this. 您应该将函数fillGrid更改为此。

function fillGrid(width, height) {
    for (i = 0; i < height; i++) {

      var tr= $("<tr>");
      for (j = 0; j < width; j++) {
        tr.append("<td><div>test</div></td>");
      };
      var table = $("<table>");
      table.append(tr);
      $(document.body).append(table);
    };
  }

Check plunker here . 在这里检查pl子。

Note: I have added sample text test so that you can see the table. 注意:我添加了示例文本测试,以便您可以看到表格。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM