简体   繁体   English

使用javascript从元素的事件中找到存在于元素数组中的html元素

[英]Use javascript to find an html element which exists in an array of elements from the element's event

I have a table generated by javascript. 我有一个由javascript生成的表格。 When creating the tbody of the table I stored each element for each cell in an array. 创建表的正文时,我将每个单元格的每个元素存储在数组中。 Any element is easily located by 任何元素都可以轻松地通过

let element = elements[r][c];

Where r and c are the row and column of the tbody. 其中,r和c是tbody的行和列。

When an event happens on the cell, like keyup , I capture the event and use event.target to retrieve the element the event was fired from. 当单元上发生事件时,例如keyup ,我捕获了事件并使用event.target来检索触发该事件的元素。

event_element = event.target;

Is there now a way to find which element in the array elements is event_element ? 现在是否可以找到数组elements中的event_element elementsevent_element

Currently I am setting element.name = element_rXcX then parsing event.target.name to find values for r and c to recover the actual element in the array. 目前,我正在设置element.name = element_rXcX然后解析event.target.name来查找rc值以恢复数组中的实际元素。

This also allows me to find the row and column of the cell in the table for an element's event. 这也使我能够在表格中找到元素事件的单元格的行和列。

I am assuming locating an element in the element array would be even better, and it may give me an aha moment understanding the DOM. 我假设在element数组中定位一个元素会更好,这可能会让我有点了解DOM。

Keep in mind the element html may often be identical, however I noticed through the chrome javascript console that if I logged an element and clicked on it in the console it would highlight the correct cell in the table. 请记住,元素html通常可能是相同的,但是我在chrome javascript控制台中注意到,如果我记录了一个元素并在控制台中单击它,它将突出显示表格中的正确单元格。

This makes me believe I must be able to look up the element in my element array. 这使我相信我必须能够在元素数组中查找元素。

You could nest Array.find like so: 您可以像这样嵌套Array.find

 var arr = [] arr[0] = ["00", "01"]; arr[1] = ["10", "11"]; arr[2] = ["20", "21"]; function find(arr, element) { var col; var row; var tmpArr = arr.find(function(subArr){ col = subArr.indexOf(element); return col > -1; }); row = arr.indexOf(tmpArr); return [row, col]; } console.log(find(arr, "21")); // [2,1] 

Bear in mind that array.find is not supported by IE, so you would need to have a polyfill for it. 请记住,IE不支持array.find ,因此您需要为其添加一个polyfill。

You can try: 你可以试试:

event_element.cellIndex and event_element.parentNode.rowIndex event_element.cellIndexevent_element.parentNode.rowIndex

The cellIndex property returns the position of a cell in the cells collection of a table row. cellIndex属性返回单元格在表行的单元格集合中的位置。 http://www.w3schools.com/jsref/prop_tabledata_cellindex.asp http://www.w3schools.com/jsref/prop_tabledata_cellindex.asp

The rowIndex property returns the position of a row in the rows collection of a table. rowIndex属性返回表在行集合中的行的位置。 http://www.w3schools.com/jsref/prop_tablerow_rowindex.asp http://www.w3schools.com/jsref/prop_tablerow_rowindex.asp

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

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