Remove image extension using jQuery

后端 未结 3 1322
北恋
北恋 2021-01-28 01:15

Is there a way to remove from img scr extension using jQuery?

meaning from this:


         


        
3条回答
  •  感动是毒
    2021-01-28 02:06

    You could do:

    $('img').each(function(){
        $(this).attr('src', $(this).attr('src').replace(/\.jpg/, ''));
    });
    

    If you have multiple extensions you need to look for you could do:

    var exts = ['.jpg', '.gif', '.png'];
    $('img').each(function(){
        var $t = $(this);
        $.each(exts, function(i,v){
            $t.attr('src', $t.attr('src').replace(v, ''));
        });
    });
    

提交回复
热议问题