-3
私はテーブルを含むWebページを持っています。そのテーブルには、私たちがサーバーで生成したセルの行があります。そのため、私は要素へのアクセスが制限されています。jQuery +要素のリストの親を取得
各行には、divを含むセルがあります。私は各divの親のセルを取得し、クラスを追加したい。これを試みるには、
私は次のものを持っています:FiddleselectAll
関数が呼び出されると、description
クラスのdiv
要素をすべて取得しようとしています。
description
をホストするtd
要素のすべてにselected
クラスを追加したいとします。
私は$('.description');
を知っています。私はこれらをループしてparent
をつかむことができました。しかし、セレクタでこれを行うより良い方法があるのだろうかと思っていました。もしそうなら、どうですか?
function selectAll() {
var divs = $('.description');
}
@import url('http://getbootstrap.com/dist/css/bootstrap.css');
.description {
background-color:#eee;
}
.selected {
padding:2rem;
background-color:yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div class="container">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Count</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>Item 1</td>
<td>3</td>
<td>
<div class="description">
Simple description
</div>
</td>
</tr>
<tr>
<td>Item 1</td>
<td>3</td>
<td>
<div class="description">
Simple description
</div>
</td>
</tr>
<tr>
<td>Item 1</td>
<td>3</td>
<td style="padding:0;">
<div class="description">
Some more text.
</div>
</td>
</tr>
</tbody>
</table>
<button class="btn btn-primary" onclick="selectAll();">
Select All
</button>
</div>
['.parent()'](https://api.jquery.com/parent/) – 4castle
['.parents()'](https://api.jquery.com/parents/) –
Googleでクイック検索を行うと、回答が –