我试图在单击按钮时在td中获取值。
我使用下面的代码,仅获得空白值。
所以我试图更改css,以查看它是否选择了特定的td。然后
$(this).parents('tr td:nth-child(8)').css('color','red');
没有工作,但
$(this).parents('tr').css('color','red');
工作并使该行数据全部变为红色。为什么这个 ?
最佳答案
第一个代码示例的问题在于,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/