简体   繁体   English

jQuery-仅更新图片网址文件夹

[英]jQuery - update image url only folder

How can I update, Image URL only some part of it... 如何更新,图片网址只是其中的一部分...

For Eg: I have 2 folders, if images coming from lenovo folder, I want change that folder to samsung 例如:我有2个文件夹,如果图像来自lenovo文件夹,我想将该文件夹更改为三星

<img src="images/lenovo/abc.jpg"> to <img src="images/samsung/abc.jpg"> <img src="images/lenovo/abc.jpg"><img src="images/samsung/abc.jpg">

 $(function() { $("img").each(function() { var imgURL = $(this).attr('src'); if(imgURL.lastIndexOf('/')>6) { imgURL = imgURL.replace("images/lenovo","images/samsung"); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <img src="images/lenovo/abc.jpg"><img src="images/xyz.jpg"> 

Take the current src of img and the use replace() to replace to update src . 使用img的当前src并使用replace()进行替换以更新src You can do it like below. 您可以按照以下步骤进行操作。 Hope this will help you. 希望这会帮助你。

$(function () {
    var img = $("img");

    img.each(function () {
        var src = $(this).attr('src');

        if (src.indexOf('lenovo/') > -1) {
            $(this).attr('src', src.replace("lenovo", "samsung"));
        }
    });
})

Check if the src-attribute contains 'lenovo'. 检查src属性是否包含“ lenovo”。

If it does, then replace it. 如果是这样,请更换它。

$(function() {
  var src = $('img:first').attr('src');
  var needle = 'lenovo';
  var altDirectory = 'samsung';
  var rep;

  if (src.search(needle) > -1) {
    rep = src.replace(needle, altDirectory);

    $('img:first').attr('src', rep);
  }       
});

Live-Demo: http://codepen.io/mizech/pen/QyNJEp 现场演示: http//codepen.io/mizech/pen/QyNJEp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM