1

これを行うためのより良い方法があるかどうかを確認するだけです。

データの行と列がいくつかある標準のHTMLテーブルがあるとします。私がやりたいのは、その列に属する各行またはセルに関数を適用することです。私が今試しているのは3深度ループであり、実際には最高ではありません。

<table id="myTable"> 
<thead> 
<tr> 
    <th>Last Name</th> 
    <th>First Name</th> 
    <th>Email</th> 
    <th>Due</th> 
    <th>Web Site</th> 
</tr> 
</thead> 
<tbody> 
<tr> 
    <td>Smith</td> 
    <td>John</td> 
    <td>jsmith@example.com</td> 
    <td>$50.00</td> 
    <td>http://www.jsmith.example.com</td> 
</tr> 
<tr> 
    <td>Bach</td> 
    <td>Frank</td> 
    <td>fbach@example.com</td> 
    <td>$50.00</td> 
    <td>http://www.frank.example.com</td> 
</tr> 
<tr> 
    <td>Doe</td> 
    <td>Jason</td> 
    <td>jdoe@example.com</td> 
    <td>$100.00</td> 
    <td>http://www.jdoe.example.com</td> 
</tr> 
<tr> 
    <td>Conway</td> 
    <td>Tim</td> 
    <td>tconway@example.net</td> 
    <td>$50.00</td> 
    <td>http://www.timconway.example.com</td> 
</tr> 
</tbody> 
</table> 

Webサイトの列文字列を大文字にするか、それを使って何かきちんとしたことをしたいとしましょう。私はおそらく:

*select the tbody using dom selector or jquery.
*for loop with each column
   *check if the data is what I want
*for loop for each row
   *do my function to each cell

これは少し遅くなる可能性があり、可能な場合は常にネストされたループを避けようとします。とにかく、Domを使用してカラムをコンテナとして扱う方法はありますか?

IE  for (cell x in Column y)
{ doMyfunction(); }
4

3 に答える 3

2

jQueryを使用している場合は、.each()関数を使用できます。

$('#myTable td').each(function () {
    // action to perform.  Use $(this) for changing each cell
});

行内の特定のセルを1つだけ変更したい場合は、それがどのセルであるかがわかったら、:eq()または:nth-​​child()セレクターを使用すると、特定のセルを他のセルなしで操作できます。そうする行。

以下のコードは、eq()のインデックスが0から始まるため、行の最初のセルを操作します。

$('#myTable tr td:eq(0)').each(function () {
    // action to perform.  Use $(this) for changing each cell
});

以下のコードは引き続き最初の子を操作しますが、nth-child()のインデックスは1から始まります

$('#myTable tr td:nth-child(1)').each(function () {
    // action to perform.  Use $(this) for changing each cell
});
于 2012-06-12T10:58:19.263 に答える
1

スタイリングしたいだけの場合は、cssを使用する方がはるかに高速です

tbody tr td:nth-child(4){font-weight:bold;}

私の知る限り、JQueryでそのcssセレクターを使用することもできます

于 2012-06-12T10:57:06.527 に答える
0

jQueryを使用する

$("td", "#myTable tbody").each(function(index, elem){
// do my stuff here
});
于 2012-06-12T10:56:16.217 に答える