如何使用jQuery選擇表列

[英]How to select a table column with jQuery


I want to select a table column and all I know is the header text of the column. (th.innerText)

我要選擇一個表列,我只知道列的標題文本。(th.innerText)

I tried the following code but it doesn't work:

我嘗試了下面的代碼,但它不起作用:

ownerIndex = $('th:contains("Owner")').index();
$('table tr td:nth-child(ownerIndex)')

any ideas?

什么好主意嗎?

3 个解决方案

#1


48  

Ok. I found a solution:

好的。我找到了一個解決方案:

$('table tr td:nth-child('+ownerIndex+')')

#2


20  

In the example above ownerIndex needs to be incremented by 1 to match the indexing of nth-child, which starts at 1 rather than 0.

在上面的示例中,所有者索引需要增加1,以匹配第n -child索引,它從1開始,而不是0。

Here's my take: http://jsfiddle.net/2xU8t/

這里是我采用的方法:http://jsfiddle.net/2xU8t/

/* Set all the cells in columns with THEHEADING in the heading to red */

// Find the heading with the text THEHEADING
columnTh = $("table th:contains('THEHEADING')");

// Get the index & increment by 1 to match nth-child indexing
columnIndex = columnTh.index() + 1; 

// Set all the elements with that index in a tr red
$('table tr td:nth-child(' + columnIndex + ')').css("color", "#F00");

// Set the heading red too!
columnTh.css("color", "#F00"); 

#3


0  

This seems to work using the back tick as opposed to a single quote:

這似乎是使用后勾,而不是單一的引用:

$(`table tr td:nth-child(${ownerIndex})`)

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/12/04/7206a41316905a21a1a3b0812a5642b7.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com