Img yo'l jquery-ni almashtirish

Img yo'lini jquery-ga almashtirishga harakat qilyapman (uzoq sahifaga in'ektsiya qilish)

replace example.com/thumbs

with example.com/images

Men buni sinab ko'rdim, lekin u ishlamayapti.

 $("img").attr("src").replace("thumbs", "images");
1
Rasm manbasini jQuery yordamida o'zgartirish mumkin dublikati
qo'shib qo'ydi muallif Mike McCaughan, manba

6 javoblar

Ushbu qiymatini oladi, ammo belgilanmaydi xususiyatiga qaytarishmaydi:

$("img").attr("src").replace("thumbs", "images");

Buning uchun yana bir qadam kerak:

var newSrc = $("img").attr("src").replace("thumbs", "images");
$("img").attr("src", newSrc);

Yoki bir qatorni xohlasangiz:

$("img").attr("src", $("img").attr("src").replace("thumbs", "images"));
3
qo'shib qo'ydi

Buni qarang, siz rasm uchun src ni o'rnatmadingiz.

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

$(function() {
      $('img').each(function() {
          $(this).attr('src', $(this).attr('src').replace('thumbs', 'imagessss')); console.log('New src: ' + $(this).attr('src'));
          });
      });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div> </div>
1
qo'shib qo'ydi

Qayta ishlangan funktsiyani attr() usulida ikkinchi argument joriy atribut qiymati saqlagan usul.

$('img').attr('src', function(i, src){ 
   return src.replace('thumbs', 'images'); 
});

Yuqorida ko'rsatilgan usul img teglari ustida bir necha img elementlari mavjud bo'lsa, uni yineleyecektir, shuning uchun har() usulini yineleme uchun.

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

setTimeout(function() {
  $('img').attr('src', function(i, src) {
    return src.replace('thumbs', 'images');
  });
}, 2000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
#
#
#
#
#
#
#
</div> </div>
0
qo'shib qo'ydi
var oldSrc = 'http://example.com/smith.gif';
var newSrc = 'http://example.com/johnson.gif';
$('img[src="' + oldSrc + '"]').attr('src', newSrc);

Nima qilishni xohlasangiz shu:

0
qo'shib qo'ydi

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

 
 var newPath = $("img").attr("src").replace("thumbs", "images");
  $("img").attr("src",newPath);
</div> </div>
0
qo'shib qo'ydi

Step 1: script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
Step 2:

    $(document).ready(function(){
         $('img').each(function() {
               $(this).attr('src', $(this).attr('src').replace('thumbs', 'images')); 
               console.log('Latest image link ' + $(this).attr('src'));
           });
      });
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