简体   繁体   English

如何在JavaScript中的动态填充表中获取td的innerHTML

[英]How to get innerHTML of td in dynamically populated table in JavaScript

I created a table and populated values from an array, so I have 5x5 table, where each td will be filled with a word. 我创建了一个表并从数组中填充了值,所以我有5x5的表,其中每个td都将填充一个单词。 The word come from array memo and all the code below works fine. 这个词来自数组memo ,下面的所有代码都可以正常工作。

var myTableDiv = document.getElementById("results")
var table = document.createElement('TABLE')
var tableBody = document.createElement('TBODY')

table.border = '1'
table.appendChild(tableBody);

//TABLE ROWS
for (i = 0; i < this.memo.length; i++) {
    var tr = document.createElement('TR');
    for (j = 0; j < this.memo[i].length; j++) {
        var td = document.createElement('TD');
        td.onclick = function () {
            check();
        }
        td.appendChild(document.createTextNode(this.memo[i][j]));
        tr.appendChild(td)
    }
    tableBody.appendChild(tr);
}  
myTableDiv.appendChild(table);

I have one question : I would like to click on the cell and get the word, which belongs to the cell. 我有一个问题:我想单击该单元格,并得到属于该单元格的单词。 For this purpose I tried onclick as I created td element 为此,我在创建td元素时尝试了onclick

   td.onclick = function () {
            check();
        }

The function check should print the innerHTML of the cell, which was clicked 功能检查应打印已单击的单元格的innerHTML

function check() {
    var a = td.innerHTML;
    console.log(a);
}

But it gives me always wrong text - the last one in the array, which was populated. 但这总是给我带来错误的文本-数组中的最后一个已填充。 How could I solve it?.. 我该怎么解决?

You always get the last td in the array because the last value that was set to td was of the last cell. 您总是会得到数组中的最后一个td ,因为设置为td的最后一个值是最后一个单元格的值。 You need to add the a parameter, say event , to onclick 's callback function, and then your clicked element will be referenced in event.target . 您需要在onclick的回调函数中添加一个参数,例如event ,然后您的clicked元素将在event.target引用。 Then you would be able to get it's innerHTML. 然后,您将能够获取它的innerHTML。

Here's why it's always giving you the first element: after the for (j = 0; ... loop is finished, the variable td will hold the value of the last element in the list. Then, when check is called, it accesses that same td variable pointing to the last element. 这就是为什么它总是为您提供第一个元素的原因: for (j = 0; ...循环完成后,变量td将保存列表中最后一个元素的值,然后在调用check时访问该元素指向最后一个元素的相同td变量。

To solve this, you can add an argument to the function to accept a specific element and log that. 为了解决这个问题,您可以在函数中添加一个参数以接受特定元素并将其记录下来。

td.onclick = function () {
  check(td);
};

// later...
function check(element) {
  var html = element.innerHTML;
  console.log(html);
}

Pass this as the parameter to function which will target the currently clicked td . this作为参数传递给函数,它将以当前单击的td目标。

 var memo = [[2,4,6]] var myTableDiv = document.getElementById("results") var table = document.createElement('TABLE') var tableBody = document.createElement('TBODY') table.border = '1' table.appendChild(tableBody); //TABLE COLUMNS var tr = document.createElement('TR'); tableBody.appendChild(tr); //TABLE ROWS for (i = 0; i < this.memo.length; i++) { var tr = document.createElement('TR'); for (j = 0; j < this.memo[i].length; j++) { var td = document.createElement('TD'); td.onclick = function () { check(this); } td.appendChild(document.createTextNode(this.memo[i][j])); tr.appendChild(td) } tableBody.appendChild(tr); } myTableDiv.appendChild(table); function check(thatTD) { var a = thatTD.innerHTML; console.log(a); } 
 <div id="results"></div> 

I would pass the innerHTML in the click itself - please see working example below, with some mock data for memo. 我会在点击本身中传递innerHTML-请参见下面的工作示例,其中包含一些用于备忘的模拟数据。

 var myTableDiv = document.getElementById("results") var table = document.createElement('TABLE') var tableBody = document.createElement('TBODY') var memo = [ ['joe', 'tom', 'pete'], ['sara','lily', 'julia'], ['cody','timmy', 'john'] ] table.border = '1' table.appendChild(tableBody); //TABLE ROWS for (i = 0; i < this.memo.length; i++) { var tr = document.createElement('TR'); for (j = 0; j < this.memo[i].length; j++) { var td = document.createElement('TD'); td.onclick = function () { check(this.innerHTML); } td.appendChild(document.createTextNode(this.memo[i][j])); tr.appendChild(td) } tableBody.appendChild(tr); } myTableDiv.appendChild(table); function check(a) { console.log(a); } 
 <div id="results"> </div> 

you can try.. 你可以试试..

td.onclick = function () {
    check();
}

to

td.onclick = function (evt) {
    var html = evt.target.innerHTML;
    console.log(html);
    check(html);  //to do something..
}

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

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