2

私はこのような要素をたくさん持っています:

<div></div>
<span></span>
<table></table>
<div></div>
<span></span>
<div></div>

divの間にテーブル要素があるかどうかを確認する必要があります。ある場合は何かを行います。

$('div').each(function () {
  if ($(this).nextUntil('div').include('table')) {
    $(this).addClass('got-a-table');
  }
}

このようなもの?includeメソッドがないことは知っていますが、必要なものを取得できるものはありますか?

ありがとう。

結果は次のようになります。

<div class='got-a-table'></div>
<span></span>
<table></table>
<div></div>
<span></span>
<div></div>

編集:迅速なテストのためのjsbin:http: //jsbin.com/aqoha/2/edit

4

2 に答える 2

2
$('div').each(function () {
  if ($(this).nextUntil('div').filter('table').length > 0) {
    $(this).addClass('got-a-table');
  }
});

include() の代わりに、filter() が必要です。

于 2010-05-14T07:13:15.043 に答える
0

試す

$('div').each(function () {
   var table = $(this).next('table');
   if (table) {
       if (table.next('div')){
            // do something.
       }       
   }
});
于 2010-05-14T07:18:23.977 に答える