1

Title 属性を行に追加したいのですが、値は、クリックしない限り、非表示になっている行の兄弟から取得されます。一部の画面にテキストを含む非表示の行がない場合に処理できるように、十分に一般的なものにしたかったのです。

機能する最初のパスは次のとおりです。

$(function() {
  $("td.commandText pre").each(function(index) {
    $(this).parent().parent().prev().attr('Title', $(this).text());
  });
});

それをよりエレガントにするためのアイデアはありますか?

以下は、私が使用している HTML テーブルの例です。

<table class="maingrid">
  <thead>
    <tr>
      <th>Session ID</th>
      <th>User Name</th>
      <th>Start Time</th>
      <th>End Time</th>
    </tr>
  </thead><tbody>
    <tr class="record">
      <td id="174">174</td>
      <td>user1</td>
      <td>8/2/2010 4:00:09 PM</td>
      <td></td>
  </tr>
  <tr>
     <td class="details hide commandText" colspan="4">
       <pre>Sample Text to show as a tooltip</pre></td>
     </tr>
  <tr>
     <td class="details hide" colspan="4" style="color:red"><pre>no errors</pre></td>
  </tr>
    <tr class="record">
      <td id="175">175</a></td>
      <td>user1</td>
      <td>8/2/2010 4:00:09 PM</td>
      <td></td>
  </tr>
  <tr>
     <td class="details hide commandText" colspan="4">
       <pre>Sample Text to show as a tooltip</pre></td>
     </tr>
  <tr>
     <td class="details hide" colspan="4" style="color:red"><pre>no errors</pre></td>
  </tr>
</tbody>
</table>
4

1 に答える 1

1

.closest()次のように、少し短くするために使用できます。

$(function() {
  $("td.commandText pre").each(function(index) {
    $(this).closest('tr').prev().attr('Title', $(this).text());
  });
});

それ以外に、ここで得られるエレガンスはあまりありません。

于 2010-08-03T18:44:24.197 に答える