<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table border="1px"> <tr id="filed"> <td id="id">ID</td> <td id="title">标题</td> <td id="category">类型</td> <td>操作</td> </tr> <tr> <td>1</td> <td>一起去看流星雨</td> <td>综艺</td> <td> <button onclick="del(this,'id')">删除(获取id)</button> </td> </tr> <tr> <td>2</td> <td>孤勇者</td> <td>激情</td> <td> <button onclick="del(this,'title')">删除(获取title)</button> </td> </tr> </table> </body> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script> function del(row, filed) { var tableHeadId = "#filed"; console.log(row); console.log(getCurrentRowFiledValue(row, filed, tableHeadId)); } //row,当前行的this对象.filed,需要获取的字段值.tableHeadId,table表头行row的ID值. function getCurrentRowFiledValue(row, filed, tableHeadId) { //获取table表头所有td var fileds = $(tableHeadId).children(); //记录表头中所有的字段 var tableHeadFileds = []; // 长度-1,是减去操作列. for (let i = 0; i < fileds.length - 1; i++) { tableHeadFileds.push(fileds[i].id) } //找到当前行,指定的字段值. for (let i = 0; i < tableHeadFileds.length; i++) { if (tableHeadFileds[i] == filed) { //返回当前行的指定字段值. return $(row).parent().parent().children()[i].innerHTML; } } } </script> </html>
标签:tableHeadFileds,获取,tableHeadId,html,filed,table,row From: https://www.cnblogs.com/leochan007/p/17300428.html