Javascript - bir elementni getElementsByClassName yordamida olib tashlash

Men JavaScript-ni juda yaxshi bilmayman, shuning uchun men siqilganman. Sahifadan birinchi sinf elementni olib tashlash kerak.

Men barcha sinf elementlarini olib tashlaydigan bu buyruqni topdim, lekin faqat bitta birini o'chirishim kerak.

var collection = window.content.document.getElementsByClassName("Links");

Array.prototype.forEach.call(collection, function(node) {
    node.parentNode.removeChild(node);
});

Shuning uchun uning sehrini qilmagan bu hiylani sinab ko'rdim

var collection = window.content.document.getElementsByClassName("Links")[0];

Shuningdek, natijalarni sinab ko'rdim

var collection = window.content.document.getElementsByClassName("Links");
var requiredElement = collection[0];
Array.prototype.forEach.call(requiredElement, function(node) {
    node.parentNode.removeChild(node);
});

shuning uchun ehtimol bu qatorda biror narsa bo'lishi kerak.

node.parentNode.removeChild(node);
0
document.querySelector (". Links") ni olib tashlang()
qo'shib qo'ydi muallif user1106925, manba

6 javoblar

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

document.querySelector(".Links").remove();
<div class="Links">1</div>
<div class="Links">2</div>
</div> </div>

va querySelector kodi .remove() ga nisbatan birinchi matchni (mavjud bo'lsa) qaytaradi, chunki bu faqat birinchi elementni o'chiradi.

1
qo'shib qo'ydi

forEach qayta qo'ng'iroq qilish funktsiyasida, node to'plamdagi oddiy ma'lumotlar. Ammo elementlar kollektsiyasi o'rniga bitta element mavjud ekan, siz o'sha elementni faqat ushbu elementda bajarishingiz mumkin:

var collection = window.content.document.getElementsByClassName("Links");
var requiredElement = collection[0];
requiredElement.parentNode.removeChild(requiredElement);
1
qo'shib qo'ydi

Chunki, har bir ish uchun qator yoki qator kabi tuzilmalar ustida ishlamaydi. Bitta element - strukturaga o'xshamaydi. Siz shunday qilishingiz mumkin:

var collection = window.content.document.getElementsByClassName("Links");
var node = collection[0];
node.parentNode.removeChild(node);
1
qo'shib qo'ydi

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

$(document).ready(function(){
  remove_first_element("hello");
});
function remove_first_element(class_name)
{
     var i=0;
     $("."+class_name).each(function(){
        if(i==0)
        {
           $(this).remove();
           i++;
         }
     });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hello">hello 1</div>
<div class="hello">hello 2</div>
</div> </div>
1
qo'shib qo'ydi

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

var elems = document.getElementsByClassName('box'), //catch all desired elements
    parent = document.getElementById('parent'); //catch the parent element

    parent.removeChild(elems[0]); //remove the first element from the parent
.box {
  height: 100px;
  width: 100px;
  background: blue;
  margin: 5px;
}
<div id='parent'>
  <div class='box'></div>
  <div class='box'></div>
  <div class='box'></div>
</div>
</div> </div>
0
qo'shib qo'ydi

agar dastlabki "bog'lanish" sinf elementini butun sahifadan olib tashlash kerak bo'lsa

       if (document.getElementsByClassName('links').length > 0) {
        document.getElementsByClassName('links')[0].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