javascript 如何在jQuery中获取我当前行上方(或之前)的表格行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7969387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:54:03  来源:igfitidea点击:

How to get table row above (or before) my current row in jQuery?

javascriptjqueryhtml

提问by foreyez

Say I have a variable which holds a table row.

假设我有一个保存表格行的变量。

How would I get the row right before it using javascript/jquery?

我如何在使用 javascript/jquery 之前获得该行?

回答by ?ime Vidas

This:

这:

var prevRow = row.previousElementSibling;

or

或者

var prevRow = $( row ).prev()[0];

([0]is used to "unwrap the jQuery object", to get the DOM reference to that row. If you want to perform jQuery methods on that row, you can just leave it inside, like $( row ).prev().addClass( '...' );and in that case you don't need a new variable.)

([0]用于“解包 jQuery 对象”,以获取对该行的 DOM 引用。如果您想在该行上执行 jQuery 方法,您可以将它留在里面,就像$( row ).prev().addClass( '...' );在这种情况下,您不需要一个新的多变的。)

回答by David says reinstate Monica

Assuming that the two rows are siblings (not contained in separate tbodyor theadelements):

假设这两行是兄弟姐妹(不包含在单独的tbodythead元素中):

$curRow = $(this); // or however you're getting the current `tr`.
$curRow.prev('tr');

Should get you the previous row.

应该让你得到前一行。

回答by Sam

Well you need to do something like so:

那么你需要做这样的事情:

$("#cellID").parent().prev()

$("#cellID").parent().prev()

回答by Exception

Your input is not sufficient but if i understand correctly here is the code for your requirement..

您的输入是不够的,但如果我理解正确,这里是您要求的代码。

If your Variable contains table row id  then $('#yourVariableName').prev('tr')[0] will work.
If your Variable contains table row Class  then $('.yourVariableName').prev('tr')[0] will work.
If your Variable contains table row index  then $(' table tr').eq(varValue).prev('tr')[0] will work. 

But please specify what your variable will contain.

但请指定您的变量将包含什么。

回答by Alucard

Let's say you want to get the input value from the td in the tr above the current one:

假设您想从当前值上方的 tr 中的 td 获取输入值:

$('#id').prev().children('td').children(':text').val();