我试图在单击 td 中的按钮时获取其中的值。 我使用下面的代码,但只得到空白值。
所以我尝试更改CSS以查看它是否选择特定的td。然后
$(this).parents('tr td:nth-child(8)').css('color','red');
没用,但是
$(this).parents('tr').css('color','red');
工作并将所有 td 中的行数据设为红色。为什么这个 ?
最佳答案
第一个代码示例的问题是 td:nth-child(8)
不是 this
中引用的元素的父元素,tr
是。
要解决此问题,您需要单独调用 parents()
和find()
,像这样:
$(this).parents('tr').find('td:nth-child(8)').css('color','red');
另请注意,假设您只想查找立即 tr
父元素,可以替换parents()
与 closest()
稍微提高性能。
关于javascript - 如何通过点击 td 按钮获取其值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38211853/