[英]Get column value onclick HTML table
I have this html table: 我有这个HTML表:
tabela 塔贝拉
|A|B|C|D|
_________
001|M|N|O|P|
002|R|S|T|U|
And with this script I can get the row 1st value, e. 使用此脚本,我可以获取第1行的值e。 onclick N get the value 001
onclick N获得值001
var table = document.getElementById("tabela");
var rows = table.rows;
for (var i = 1; i < rows.length; i++) {
rows[i].onclick = (function() {
var rowid = (this.cells[0].innerHTML);
window.location.href = "next.php?rowidphp="+ rowid;
});
}
The thing is that I need to get the column 1st value, e. 关键是我需要获取列的第一个值e。 onclick N shuld get the value B
onclick N应该得到值B
I'm trying everything but I can reach the point..... 我正在尝试一切,但我可以达到目的。
Here's the fiddle: http://jsfiddle.net/t7G6K/ 这是小提琴: http : //jsfiddle.net/t7G6K/
var table = document.getElementById("tabela");
var rows = table.rows;
for (var i = 1; i < rows.length; i++) {
rows[i].onclick = (function (e) {
var rowid = (this.cells[0].innerHTML);
var j = 0;
var td = e.target;
while( (td = td.previousElementSibling) != null )
j++;
alert(rows[0].cells[j].innerHTML);
});
}
jsFiddle http://jsfiddle.net/2dAkj/9/# jsFiddle http://jsfiddle.net/2dAkj/9/#
Ok with jQuery i would do it like this. 好的jQuery我会这样做。
$(function () {
$('table').on('click', function (e) {
var x = $(e.target);
var index = x.parents('tr').find('td,th').index(x);
alert($(x.parents('table').find('tr').first().find('td,th')[index]).text());
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.