سؤال

لدي مجموعة من العناصر مثل هذا:

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

أحتاج إلى التحقق مما إذا كان هناك عنصر الجدول بين DIVs أم لا ، وإذا كان الأمر كذلك.

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

شيء من هذا القبيل؟ أعلم أنه لا يوجد طريقة ، هل هناك شيء يمكن أن يحصل على ما أحتاجه؟

شكرًا.

يجب أن تكون النتيجة هكذا:

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

تحرير: jsbin لـ Quick Testign: http://jsbin.com/aqoha/2/edit

هل كانت مفيدة؟

المحلول

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

بدلاً من تضمين () ، تريد Filter ().

نصائح أخرى

محاولة

$('div').each(function () {
   var table = $(this).next('table');
   if (table) {
       if (table.next('div')){
            // do something.
       }       
   }
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top