[英]add several event listeners to table
I have a table from which I want to handle click events on each row, depending on the contents of a table cell. 我有一个表格,我想根据表格单元格的内容处理每行的单击事件。 The table is (currently) filled dynamically from a static array, although later it will be filled from a database:
该表(当前)是从静态数组动态填充的,尽管稍后它将从数据库填充:
var routeTable = document.getElementById('routeTable');
var routes = [
['..', '..', '..', "url_1"],
['..', '..', '..', "url_2"],
['..', '..', '..', "url_3"],
['..', '..', '..', "url_4"],
]
for (r = 0; r < routes.length; r++) {
route = routes[r];
row = routeTable.insertRow(r+1);
....
....
row.addEventListener("click", function () { display_gpx(route[3]);
});
}
The trouble is that the parameter passed to the function display_gpx() is always the value "url_4" (from the last route in the table). 问题在于传递给函数display_gpx()的参数始终为值“ url_4”(来自表中的最后一条路由)。
I found that the elements for clicking must exist when the document is created , so I looked here (the pure js version) and tried adding 我发现在创建文档时,用于单击的元素必须存在,因此我在这里查看了 (纯js版本)并尝试添加
row.className = "myClick";
and changing the eventListener to 并将eventListener更改为
document.addEventListener("click", function (e)
{
if (e.target.classList.contains("myClick"))
display_gpx(route[3]);
},false);
but the events don't fire at all. 但是这些事件根本不会触发。
I think I've solved this before, but returning to javascript after a long break and can't remember the solution! 我想我已经解决了这个问题,但是经过很长的休息后又回到了javascript,不记得解决方案了!
Try this: 尝试这个:
(function(url) {
row.addEventListener("click", function () { display_gpx(url); });
})(route[3]);
Edit: Snippet replicating the problem with simple code: 编辑:用简单的代码片段复制问题:
var routes = [ ['..', '..', '..', "url_1"], ['..', '..', '..', "url_2"], ['..', '..', '..', "url_3"], ['..', '..', '..', "url_4"], ] for (r = 0; r < routes.length; r++) { route = routes[r]; setTimeout(function () { console.log("not working: " + route[3]); }, 100); } for (r = 0; r < routes.length; r++) { route = routes[r]; (function(url) { setTimeout(function () { console.log("working: " + url); }, 100); })(route[3]); }
You mised to add the class myClick to last cell of each row. 您误将类myClick添加到每行的最后一个单元格。
The snippet (click on the url...): 代码段(点击网址...):
var routeTable = document.getElementById('routeTable'); var routes = [ ['..', '..', '..', "url_1"], ['..', '..', '..', "url_2"], ['..', '..', '..', "url_3"], ['..', '..', '..', "url_4"], ] for (r = 0; r < routes.length; r++) { route = routes[r]; row = routeTable.insertRow(r); for(i=0; i<route.length; i++) { newCell = row.insertCell(); newText = document.createTextNode(route[i]) newCell.appendChild(newText); if (i == 3) { newCell.classList.add('myClick') } } } document.addEventListener("click", function (e) { if (e.target.classList.contains("myClick")) console.log('classList is: ' + e.target.classList + ' element text is: ' + e.target.textContent); },false);
<table id="routeTable"> <tbody> </tbody> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.