JavaScript 使用 (this) 关键字

标签 javascript html function html-table this

我正在创建一个具有多个表的应用程序,一个表包含客户及其位置,另一个表包含客户及其库存商品和商品 ID。我在创建可用于多个表的函数时遇到问题,该函数允许用户为表创建新行,并且我有工作代码,但使用该代码我必须为每个唯一的表复制它。

我想要的是使用(this)以便一个函数适用于多个表,我尝试使用(this)但它显然无法工作,第一个问题是它无法读取属性“length”对于 table ,但我确信我会遇到更多问题。

如有任何帮助,我们将不胜感激。

期望的结果:使用(this)在多个表上使用一个函数。

function appendRow() {
        var table = document.getElementsByTagName('table'); // table reference
        length = this.length, 
        row = this.insertRow(this.rows.length),      // append table row
        i;
    // insert table cells to the new row
    for (i = 0; i < this.rows[0].cells.length; i++) {
        createCell(row.insertCell(i), i, 'row');
    }
}


// this works, but if I want it for multiple tables, I must copy it for each table....
/*
function appendRow() {
    var custList = document.getElementById('custList'), // table reference
        row = custList.insertRow(custList.rows.length),      // append table row
        i;
    // insert table cells to the new row
    for (i = 0; i < custList.rows[0].cells.length; i++) {
        createCell(row.insertCell(i), i, 'row');
    }
}
*/ 


function createCell(cell, text, style) {
    var div = document.createElement('div'), // create DIV element
        txt = document.createTextNode('_'); // create text node
    div.appendChild(txt);               // append text node to the DIV
    div.setAttribute('id', style);        // set DIV class attribute
    div.setAttribute('idName', style);    // set DIV class attribute for IE (?!)
    cell.appendChild(div);                   // append DIV to the table cell
}
<div id="custDiv">
  <div class="addBtns">
    <input id="searchName" onkeyup="custSearchName()" type="text" placeholder="search name"></input>
  </div>
  <div style="width: 355px; margin: 0 auto; height: 50px;">
    <button id="addCust" onclick="appendRow(this)">add customer</button>
  </div>

  <div class="custScroll">
    <table id="custListTop" contenteditable="false">
      <tr>
        <td style="border-top-left-radius: 5px;">Customers</td>
        <td style="border-top-right-radius: 5px;">Main Location</td>
      </tr>
    </table>
    <table id="custList" contenteditable="true">
      <tr>
        <td>Someone</td>
        <td>something</td>
      </tr>
    </table>
  </div>
</div>

最佳答案

我问你为什么要使用“this”关键字。在按钮中说“这个”是没有意义的,除非你以某种方式告诉它“这个”是什么。否则它无法知道“this”实际上是一个名为 custList 的表。您需要以某种方式建立该连接,并且我选择在下面的代码片段中将表名称作为 js 函数中的参数传递。

您的代码中还存在一个问题,即未声明 i 变量。

function appendRow(id) {
        var table = document.getElementById(id); // table reference
        length = table.length, 
        row = table.insertRow(table.rows.length);      // append table row
        var i;
    // insert table cells to the new row
    for (i = 0; i < table.rows[0].cells.length; i++) {
        createCell(row.insertCell(i), i, 'row');
    }
}

function createCell(cell, text, style) {
    var div = document.createElement('div'), // create DIV element
        txt = document.createTextNode('_'); // create text node
    div.appendChild(txt);               // append text node to the DIV
    div.setAttribute('id', style);        // set DIV class attribute
    div.setAttribute('idName', style);    // set DIV class attribute for IE (?!)
    cell.appendChild(div);                   // append DIV to the table cell
}
<div id="custDiv">
  <div class="addBtns">
    <input id="searchName" onkeyup="custSearchName()" type="text" placeholder="search name"></input>
  </div>
  <div style="width: 355px; margin: 0 auto; height: 50px;">
    <button id="addCust" onclick="appendRow('custList')">add customer</button>
  </div>

  <div class="custScroll">
    <table id="custListTop" contenteditable="false">
      <tr>
        <td style="border-top-left-radius: 5px;">Customers</td>
        <td style="border-top-right-radius: 5px;">Main Location</td>
      </tr>
    </table>
    <table id="custList" contenteditable="true">
      <tr>
        <td>Someone</td>
        <td>something</td>
      </tr>
    </table>
  </div>
</div>

关于JavaScript 使用 (this) 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47227027/

相关文章:

javascript - 根据时间值更改文本颜色php

C++:类之间的 "unresolved overload function type"

javascript - HTTP 错误 414。请求 URL 太长

javascript - 一个接一个地调用一个函数已经在本地存储中设置了项目

javascript - 循环ajax调用来填充多个div

html - 如何使用 CSS 将选择下拉列表中的文本对齐方式移向顶部和左侧?

jquery - 函数返回零如何解决这个问题?

javascript - 如何为 chrome 和 ie favicon(加载指示器)设置动画

javascript - 使用 JS 和 HTML 更改样式有效,JS 和 CSS 并不总是有效

javascript - Javascript onclick 事件仅在第二次点击时触发