25
テーブルの列を選択したいのですが、わかっているのは列のヘッダーのテキストだけです。 (th.innerText)jQueryで表の列を選択する方法
私は、次のコードを試してみましたが、それは動作しません:
ownerIndex = $('th:contains("Owner")').index();
$('table tr td:nth-child(ownerIndex)')
任意のアイデア?
テーブルの列を選択したいのですが、わかっているのは列のヘッダーのテキストだけです。 (th.innerText)jQueryで表の列を選択する方法
私は、次のコードを試してみましたが、それは動作しません:
ownerIndex = $('th:contains("Owner")').index();
$('table tr td:nth-child(ownerIndex)')
任意のアイデア?
ownerIndex上記の例では
$('table tr td:nth-child('+ownerIndex+')')
は、n番目の子のインデックスに一致するように1ずつインクリメントされる必要があり、1ではなく0
で始まるここに私のテイクです:私は解決策を見つけた 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");
これはすべてのセルがcolspan = 1を持つ場合に機能しますが、セルの列スパンが異なる場合はブレークします。 –
ありがとうございます。「テーブルレイアウト:固定」スタイルの列幅を設定するためにあなたのアイデアを追加しました。この場合、私はcolgroupから子供を選択します。 $( '#plGrid colgroup col:nth-child(0)')。css( 'width'、 '150px'); $( '#plGrid colgroup col:nth-child(1)')。css( 'width'、 '250px'); –
これをnth-child(1)にします。 nth-child(2) –