简体   繁体   English

如何用Javascript旁边的元素的值更改单元格的元素?

[英]How to change the element of a cell with the value of the element next to it in Javascript?

In table like the one below in the code snippet I would like that once clicked an item, I want to change the name of the selected cell to the next name corresponding to the sequence like in the array. 在像下面代码片段中的表格中,我希望单击一个项目后,想要将所选单元格的名称更改为与数组中的序列相对应的下一个名称。 [square, triangle, circle, oval, pentagon] So if I click "square", now the name appearing on it should be "triangle". [正方形,三角形,圆形,椭圆形,五边形]因此,如果单击“正方形”,现在出现在其上的名称应为“三角形”。

 var card = [ {name:'square'}, {name:'triangle'}, {name:'circle'}, {name:'oval'}, {name:'pentagon'} ]; function generateTable(grid, rows, cols) { var row; var cells = rows * cols; for(var i=0; i < cells; i++){ // track row length and insert new ones when necessary // also creates the first row if(i % cols == 0) { row = grid.insertRow(-1); } // track our position in the card list // modulo operator lets us loop through the cards repeatedly var thisCard = card[i % card.length]; cell = row.insertCell(-1); cell.innerHTML = thisCard.name; cell.style.backgroundColor = '#D3D3D3'; } } generateTable(document.getElementById('grid'), 7, 7); 
 <table id="grid"> </table> 

You can access cells by their cellIndex property. 您可以通过其cellIndex属性访问单元格。 So once you have the cell that was clicked on, get the cell to the right (if it exists) and update the innerHTML of the clicked on cell. 因此,一旦有了被单击的单元格,请将单元格移到右侧(如果存在)并更新被单击的单元格的innerHTML。

 function changeName(e){ // Get the element that was clicked on var cell = e.target; var row, index; // If it's a td, update the innerHTML if (cell && cell.tagName.toLowerCase() == 'td') { // Get the row that the cell is in row = cell.parentNode; // Get index of cell to right index = cell.cellIndex + 1; // Make sure cell to right exists if (row.cells[index]) { // Update clicked on cell cell.innerHTML = row.cells[index].innerHTML; } } } window.onload = function(){ document.querySelector('table').addEventListener('click',changeName); } 
 td { width: 5em; border: 1px solid #999999; } 
 <table> <tr><td>square<td>triangle<td>circle<td>oval<td>pentagon </table> 

Here is a more concise version: 这是一个更简洁的版本:

 window.onload = function() { document.querySelector('table').addEventListener('click', function(e) { var cell = e.target; var next = cell.cellIndex === undefined? null : cell.parentNode.cells[cell.cellIndex + 1]; if (next) cell.innerHTML = next.innerHTML }); }; 
 td { width: 5em; border: 1px solid #999999; } 
 <table> <tr><td>square<td>triangle<td>circle<td>oval<td>pentagon </table> 

Edit 编辑

Updated to loop through the names in succession 更新以连续遍历名称

 var card = [ {name:'square'}, {name:'triangle'}, {name:'circle'}, {name:'oval'}, {name:'pentagon'} ]; function getNextCard(name) { } window.onload = function() { document.querySelector('table').addEventListener('click', function(e) { var node = e.target; var name = node.textContent; var index; if (node.tagName.toLowerCase() == 'td') { index = (card.findIndex(function(obj){ return obj.name == name; }) + 1) % card.length; node.textContent = card[index].name; } }); }; 
 td { width: 5em; border: 1px solid #999999; } 
 <table> <tr><td>square<td>triangle<td>circle<td>oval<td>pentagon </table> 

"I want the value to change to the next in the sequence" (clarification from a comment that it isn't the element to the right that matters, it's the sequence in the array) “我希望将值更改为序列中的下一个” (注释中的说明是,与元素无关的不是右边的元素,而是数组中的序列)

OK, so I would probably use a (delegated) click handler attached to the table element. 好的,所以我可能会使用附在表元素上的(委托)单击处理程序。 Get the value of the clicked td element and look it up in the card array, then from there get the next item from the array. 获取被单击的td元素的值,并在card阵列中查找它,然后从那里获取阵列中的下一项。 Maybe a little something like this: 也许是这样的:

 document.getElementById('grid').addEventListener("click", function(e) { if (e.target.nodeName.toLowerCase() === "td") { var currentIndex = card.findIndex(function(shape) { return shape.name === e.target.innerHTML; }); e.target.innerHTML = card[(currentIndex + 1) % card.length].name; } }); var card = [ {name:'square'}, {name:'triangle'}, {name:'circle'}, {name:'oval'}, {name:'pentagon'} ]; function generateTable(grid, rows, cols) { var row; var cells = rows * cols; for(var i=0; i < cells; i++){ // track row length and insert new ones when necessary // also creates the first row if(i % cols == 0) { row = grid.insertRow(-1); } // track our position in the card list // modulo operator lets us loop through the cards repeatedly var thisCard = card[i % card.length]; cell = row.insertCell(-1); cell.innerHTML = thisCard.name; cell.style.backgroundColor = '#D3D3D3'; } } generateTable(document.getElementById('grid'), 7, 7); 
 <table id="grid"> </table> 

I have used the array .findIndex() method to find the item in the array, so if you want to support IE you'll need a polyfill , or of course you could just use a for loop or whatever. 我已经使用了数组.findIndex()方法在数组中查找项目,因此,如果要支持IE,则需要使用polyfill ,或者当然也可以使用for循环或其他方法。

Please feel free to optimize this solution. 请随时优化此解决方案。 Hope this works for you and this is what you want :) 希望这对您有用,这就是您想要的:)

 var card = [{ name: 'square' }, { name: 'triangle' }, { name: 'circle' }, { name: 'oval' }, { name: 'pentagon' } ]; function onCellClick(td) { value = td.innerHTML.trim(); for (var i = 0; i < card.length; i++) { var index; if (card[i].name === value) { index = i + 1; if (card.length - 1 === i) { index = 0 } td.innerHTML = card[index].name; break; } } } function generateTable(grid, rows, cols) { var row; var cells = rows * cols; for (var i = 0; i < cells; i++) { // track row length and insert new ones when necessary // also creates the first row if (i % cols == 0) { row = grid.insertRow(-1); } // track our position in the card list // modulo operator lets us loop through the cards repeatedly var thisCard = card[i % card.length]; cell = row.insertCell(-1); cell.innerHTML = thisCard.name; cell.onclick = function() { onCellClick(this); }; cell.style.backgroundColor = '#D3D3D3'; } } generateTable(document.getElementById('grid'), 7, 7); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="grid"> </table> 

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

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