JavaScript tomonidan barcha <img> ga o'zgartirishlar kiritilsin

Bir tugmani bosib sahifadagi filter kodini barcha img belgilariga majbur qilishni xohlayman. Rasmlar soni turli xil sahifalarda farq qiladi. Men bu erga keldim:

function ccbfunction(wt) {
    var w = document.getElementsByTagName("img");
    var z;
    if (wt == 0) {
        z = "#e0e0e0";
    } else {
        z = "#fff";
    };
    w[0].style.filter = "brightness(80%)";
}

Endi u faqat birinchi tasvir ustida ishlaydi va men butun ni qanday tanlashni bilmayman. Tasvirlar soni doimiy emas, shuning uchun for loop foydalanmang.

2
@Exception_al Boshqa variantim bo'lmasa.
qo'shib qo'ydi muallif ata, manba
@Exception_al Ha sizning ishingiz, rahmat.
qo'shib qo'ydi muallif ata, manba
JQuery-ni ishlatadimi? hayoting shu qadar sodda bo'lar edi ...
qo'shib qo'ydi muallif Exception_al, manba

6 javoblar

document.getElementsByTagName ("img") dan foydalanib, img elementlarini qator o'xshash tugunlar ro'yxatiga kiritishni taklif qilaman.

Keyin Array # dan foydalanib, uni arrayga aylantirib, Array # forEach dan foydalaning va uni har bir elementga yangi uslubi qo'llang.

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

function ccbfunction(wt) {
  var w = document.getElementsByTagName("img");
  var z;

  if (wt == 0) {
    z = "#e0e0e0";
  } else {
    z = "#fff";
  };
  Array.from(w).forEach(v => v.style.filter = "brightness(40%)");
}

ccbfunction();




</div> </div>
2
qo'shib qo'ydi

Quyidagi mantiqdan foydalanasiz.

   $('img').each(function(){
      //Write your logic here
    });
2
qo'shib qo'ydi
OP ga ko'ra, jQuery dan foydalangan holda tanlov emas!
qo'shib qo'ydi muallif tanmay, manba

Quyidagi kabi narsalarni yineleyebilirsiniz:

var w= document.getElementsByTagName("img");
for (var i = 0; i < w.length; i++) {
   //Perform activity on list[i]
    w[i].style.filter="brightness(80%)";
}
1
qo'shib qo'ydi

Buni faqat sinash mumkin

for(var i=0;i

Bu yordam berish kerak deb o'ylayman;

1
qo'shib qo'ydi

document.getElementsByTagName returns a nodeList an array like object, however if you want to iterate over nodeList, you can use a workaround to use Array methods like forEach on nodeList.

Array.prototype.forEach.call dan foydalanib, Array usullarini nodeList-da foydalanishingiz mumkin.

Quyidagi kodni ko'ring:

function ccbfunction(wt) {
    var w = document.getElementsByTagName("img");
    var z;
    if (wt == 0) {
        z = "#e0e0e0";
    } else {
        z = "#fff";
    };
    Array.prototype.forEach.call(w, function(node, index) {
        node.style.filter = "brightness(80%)";
    })
    w[0].style.filter = "brightness(80%)";
}
0
qo'shib qo'ydi

ForEach döngüsü uchun foydalanishingiz mumkin:

w.forEach(function(image) {

 image.style.filter = "brightness(80%)";

});

Bu har bir misolni chizib qo'yadi, shuning uchun sahifada qancha img belgisi mavjudligi muhim emas

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