JQuerydan so'ng elementni va keyingi elementni qanday o'chirish mumkin?

Elementdan va elementdan keyin uni olib tashlashga harakat qilaman. Nima uchun bu ishlamayapti?

$('ul').find('li').first().remove().next().remove();

<div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="false"> <div class="snippet-code snippet-currently-hidden">

$('ul').find('li').first().remove().next().remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  • One
  • Two
  • Three
</div> </div>

Bu mening loyihamda bajarishga harakat qiladigan hwatning namunasidir. Topish usulini ishlatishim kerak, bu qismni o'zgartira olmaydi. Rahmat

3

7 javoblar

Birinchi ikkita tanlovni tanlab olishingiz mumkin, keyin ularni olib tashlashingiz mumkin:

$('ul').find('li').slice(0, 2).remove();
4
qo'shib qo'ydi

Birinchi element o'chirildi, shuning uchun keyingi() funktsiyasi bo'sh to'plam oladi va hech narsa bo'lmaydi.

To'g'ri yo'l:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var $deleteMe1 = $('ul').find('li').first();
var $deleteMe2 = $deleteMe1.next();
$($deleteMe1).add($deleteMe2).remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  • One
  • Two
  • Three
</div> </div>

1
qo'shib qo'ydi

Siz olib tashlangan elementning keyingi elementini tanlashga urinasiz. : lt() tekshirgichidan indeksdagi elementni tanlang yozma indeksdan kam.

$('ul').find('li:lt(2)').remove();

<div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="false"> <div class="snippet-code snippet-currently-hidden">

$('ul li:lt(2)').remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  • One
  • Two
  • Three
</div> </div>

1
qo'shib qo'ydi

Buning sababi ishlamaydi, chunki dastlabki kod o'chirildi, shuning uchun next() ishlamaydi.

Sinash:

$('ul').children().slice(0,2).remove();
1
qo'shib qo'ydi

Sizning shaxsiy holingizda, elclanrs »hal qilish yo'lidir.

Keyingi elementni o'chirishni istagan umumiy holatida boshlang'ich tanlovchiga mos kelmasa ham add va next dan foydalanishingiz mumkin:

var toRemove = $("selector for the matching thing");
toRemove.add(toRemove.next()).remove();

<div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="false"> <div class="snippet-code snippet-currently-hidden">

setTimeout(function() {
  var toRemove = $('.m');
  toRemove.add(toRemove.next()).remove();
}, 1500);
<div>I don't match, so I'll stay</div>
<div class="m">I match, so I'll go</div>
I'm the next element after the match, so I'll go to
<div>I'm unrelated, so I'll stay</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div> </div>
0
qo'shib qo'ydi

you could use addBack()

$('ul').find('li').first().next().addBack().remove()

Litseyda bu juda mantiqqa to'g'ri kelmaydi, chunki bu birinchi ikki litsni qabul qilishning murakkab usuli. Ammo, agar biz arbitr tugunlari haqida gapiradigan bo'lsak, bu "bu tugunni keyingi tanlashning eng oddiy yo'li bo'lar edi va ularni olib tashlash

0
qo'shib qo'ydi

Bir vaqtning o'zida avval va keyingi narsani olib tashlashingiz kerak.

Shunday qilib:

 $('ul li:eq(0), ul li:eq(1)').remove(); 
0
qo'shib qo'ydi
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb