Code html
1 2 3 4 |
/*Chèn hình thêm class "sp_img"*/ <img src="link hình 1" alt="img1" class="sp_img"> <img src="link hình 2" alt="img2" class="sp_img"> <img src="link hình 3" alt="img3" class="sp_img"> |
Code jquery
1 2 3 4 5 6 7 8 9 10 11 12 |
if($(window).width()<767){ $('img.sp_img').each(function (i) { var src = $(this).attr('src'); var newsrc ; if (src.indexOf('.png') == -1) { newsrc = src.replace(".jpg", "-sp.jpg"); }else{ newsrc= src.replace(".png", "-sp.png"); } $(this).attr('src',newsrc); }); }; |
Ví dụ