【问题标题】:Filter the output images links with javascript使用 javascript 过滤输出图像链接
【发布时间】:2013-06-07 21:10:01
【问题描述】:

我在 wordpress 中使用 prettyPhoto 灯箱

我需要显示 150 像素的 wordpress 画廊缩略图而不是默认的 prettyPhoto 缩略图(他们使用大图像作为缩略图)

这是创建缩略图的代码

for (var i=0; i < pp_images.length; i++) {
    if(!pp_images[i].match(/\b(jpg|jpeg|png|gif)\b/gi)){
        classname = 'default';
        img_src = '';
    }else{
        classname = '';
        img_src = pp_images[i];
    }
    toInject += "<li class='"+classname+"'><a href='#'><img src='" + img_src + "' width='75' height='75' alt='' /></a></li>";
};

这是图片链接的输出

<img src="http://127.0.0.1/wordpress/photopname1.jpg" width="75" height="75" alt="">
<img src="http://127.0.0.1/wordpress/photopname2.gif" width="75" height="75" alt="">
<img src="http://127.0.0.1/wordpress/photopname3.png" width="75" height="75" alt="">


我需要输出是这样的

<img src="http://127.0.0.1/wordpress/photopname1-150x150.jpg" width="75" height="75" alt="">
<img src="http://127.0.0.1/wordpress/photopname2-150x150.gif" width="75" height="75" alt="">
<img src="http://127.0.0.1/wordpress/photopname3-150x150.png" width="75" height="75" alt="">

在图片扩展“-150x150”之前添加

谢谢你:)

【问题讨论】:

    标签: javascript jquery prettyphoto


    【解决方案1】:

    处理多个.s的解决方案

    var size = '-150x150';
    
    
    
    for (var i=0; i < pp_images.length; i++) {
      if(!pp_images[i].match(/\b(jpg|jpeg|png|gif)\b/gi)){
          classname = 'default';
          img_src = '';
      }else{
          classname = '';
          img_src = pp_images[i];
      }
    
      //ex: img_src = a.b.png
      var src = img_src.split('.'); //ex: ['a', 'b', 'png']
      src[src.length - 2] = src[src.length - 2] + size; //ex: ['a', 'b-150x150', 'png']
      src = src.join('.');//a.b-150x150.png
    
      toInject += "<li class='"+classname+"'><a href='#'><img src='" 
               + src + "' width='75' height='75' alt='' /></a></li>";
    };
    

    【讨论】:

      【解决方案2】:

      如果我理解你的话,你只需要这个:

       toInject += "<li class='"+classname+"'><a href='#'><img src='" + img_src.split(".").join("-150x150.") + "' width='75' height='75' alt='' /></a></li>";
      

      问题编辑后更新: 您是否可以像这样在最后添加完整路径:

       toInject += "<li class='"+classname+"'><a href='#'><img src='http://127.0.0.1/wordpress/" + img_src.split(".").join("-150x150.") + "' width='75' height='75' alt='' /></a></li>";
      

      或者您的 pp_images 数组已经包含完整路径?

      【讨论】:

      • 我正要提供这个确切的答案。
      • 是的,它可以工作,但我忘记将图像的整个链接与域名127.0.0.1/wordpress/photopname1.jpg 现在输出这样的127-150x150.0-150x150.0-150x150.1/wordpress/…
      • 我知道域名有 ip 会出现问题,但对于 foo.com 输出也会是 foo-150x150.com
      • @JimmyTodd 我已经编辑了我的答案。如果不相关,请参阅 Tamil Vendhan 的建议
      • 你为什么还要使用127.0.0.1?不是localhost 的忠实粉丝?
      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 2014-12-27
      • 1970-01-01
      • 1970-01-01
      • 2018-02-04
      • 2011-04-24
      • 2016-05-07
      • 1970-01-01
      相关资源
      最近更新 更多