我有一个包含 3 行的表,每行都有 href。单击这些链接时,我需要获取单击链接的行的完整值。
Data
Test1_link image_link view-link
Test2_link image_link view-link
上面的数据是链接,点击任何一个链接,都会渲染出对应的行值。 谁能建议我如何实现这一目标?
谢谢。
最佳答案
您可以在表格后面立即放置一个简单的脚本:
<script>
(function (d) {
// Get the table element
var table = function() {
var tables = d.getElementsByTagName('table');
return tables[tables.length - 1];
}();
table.onclick = function (e) {
var target = e.target;
// If an <a> was clicked
if( target.nodeName.toLowerCase() === 'a' ) {
// Find the tr containing that a
var node = target;
while (node.nodeName.toLowerCase() !== 'tr' )
node = node.parentNode;
var tr = node;
// Output the contents of the table cells in that row
var cells = tr.cells;
for (var i = 0; i < cells.length; i++ )
console.log(cells[i].innerHTML);
// Return false to prevent the link from loading
return false;
}
}
})(document)
</script>
关于javascript - 如何在单击链接时获取行值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23498145/