简体   繁体   English

将 jQuery .html 转换为 vanilla js

[英]Convert jQuery .html to vanilla js

I am converting a project away from jQuery and I thought this would be trivial...How do I convert:我正在从 jQuery 转换一个项目,我认为这将是微不足道的......我如何转换:

let tb = $("#design_table tbody");
tb.html("");

I tried:我试过了:

let tb = document.querySelector("#design_table tbody");
tb.innerHTML = "";
  

When I use the above my table becomes:当我使用上面的表格时,我的表格变为:

[object Object][object Object][object Object][object Object]...etc

There is only 1 table with tbody tag只有 1 个带有 tbody 标签的表

Edit: The querySelector appears to be selecting the table:编辑: querySelector 似乎正在选择表格:

<tbody>
  <tr title="row_0">
    <td title="col_0" class="material_0 rotate_0"></td>
    <td title="col_1" class="material_0 rotate_0"></td>
    <td title="col_2" class="material_0 rotate_0"></td>
    <td title="col_3" class="material_0 rotate_0"></td>
    <td title="col_4" class="material_0 rotate_0"></td>
...etc.

This works, but still half jQuery:这有效,但仍然是 jQuery 的一半:

let tb = $("#design_table tbody");
tb.innerHTML;

Code :代码

function reDraw () {
      let tb = $("#design_table tbody");
      tb.html("");
      for (var y = 0; y < MCD.settings.fieldHeight; y += 1) {
        var new_row = $("<tr>", {
          title: "row_" + y
        });
        for (var x = 0; x < MCD.settings.fieldWidth; x += 1) {
          let new_cell = $("<td>", {
            title: "col_" + x,
            //class: "material_" + MCD.matrix[y][x] + " rotate_0",
            class: "material_" + MCD.matrix[y][x][0] + " rotate_" + MCD.matrix[y][x][1],
          });
    new_row.append(new_cell);
        }
        tb.append(new_row);
      }
  }     

Update : For:更新:对于:

let tb = $("#design_table tbody");
      console.log(tb)
      tb.html("");
      let ab = document.querySelectorAll("#design_table tbody");
      console.log(ab[0]);

Console.log returns: Console.log 返回:

d { 0: HTMLTableSectionElement, length: 1, prevObject: d, context: HTMLDocument, selector: "#design_table tbody" }
<tbody>
  <tr title="row_0">
    <td title="col_0" class="material_0 rotate_0"></td>
    <td title="col_1" class="material_0 rotate_0"></td>
    <td title="col_2" class="material_0 rotate_0"></td>
    <td title="col_3" class="material_0 rotate_0"></td>
    <td title="col_4" class="material_0 rotate_0"></td>
    <td title="col_5" class="material_0 rotate_0"></td>
    <td title="col_6" class="material_0 rotate_0"></td>
    <td title="col_7" class="material_0 rotate_0"></td>
    <td title="col_8" class="material_0 rotate_0"></td>
    <td title="col_9" class="material_0 rotate_0"></td>
    <td title="col_10" class="material_0 rotate_0"></td>
    <td title="col_11" class="material_0 rotate_0"></td>
    <td title="col_12" class="material_0 rotate_0"></td>
    <td title="col_13" class="material_0 rotate_0"></td>
    <td title="col_14" class="material_0 rotate_0"></td>
    <td title="col_15" class="material_0 rotate_0"></td>
    <td title="col_16" class="material_0 rotate_0"></td>
    <td title="col_17" class="material_0 rotate_0"></td>
    <td title="col_18" class="material_0 rotate_0"></td>
    <td title="col_19" class="material_0 rotate_0"></td>
  </tr>
...etc...
</tbody>

Obviously two different results, but I don't understand why.显然两个不同的结果,但我不明白为什么。

Roughly, it translates like this:大致翻译如下:

function reDraw () {
  //let tb = $("#design_table tbody");
  let tb = document.querySelector('#design_table tbody');

  //tb.html('');
  tb.innerHTML = '';

  for (var y = 0; y < MCD.settings.fieldHeight; y += 1) {

    //var new_row = $("<tr>", {
    //  title: "row_" + y
    //});
    var new_row = document.createElement('tr');
    new_row.title = 'row_' + y;

    for (var x = 0; x < MCD.settings.fieldWidth; x += 1) {

      //let new_cell = $("<td>", {
      //  title: "col_" + x,
      //  class: "material_" + MCD.matrix[y][x][0] + " rotate_" + MCD.matrix[y][x][1],
      //});
      let new_cell = document.createElement('td');
      new_cell.title = 'col_' + x;
      new_cell.className = 'material_' + MCD.matrix[y][x][0] + ' rotate_' + MCD.matrix[y][x][1];

      new_row.append(new_cell);
    }
    tb.append(new_row);
  }
}

This isn't an exact translation.这不是准确的翻译。 For example, jQuery creates wrappers for the elements and passes those around.例如,jQuery 为元素创建包装器并传递它们。

After clean up and optimizing, it looks like this:清理优化后如下图:

function reDraw () {
  const html = [];

  for (var y = 0; y < MCD.settings.fieldHeight; y += 1) {
    html.append(`<tr title="row_${y}">`);

    for (var x = 0; x < MCD.settings.fieldWidth; x += 1) {
      html.append(`<td title="col_${x}" class="material_${MCD.matrix[y][x][0]} rotate_${MCD.matrix[y][x][1]}"></td>`);
    }

    html.append('</tr>');
  }

  const tb = document.querySelector('#design_table tbody');
  tb.innerHTML = html.join('');
}

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

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