在Shopify中获取博客文章的图像

时间:2016-05-18 10:31:07

标签: javascript jquery shopify

我必须使用博客修改Shopify网站,该博客使用{{ article.content }}提取博客文章的内容。此方法获取文章的全部内容,但我需要单独获取图像。我不是指“主要”图像,而是指博客文章内容所包含的所有图像。我怎样才能做到这一点?如果我能得到一个包含图像路径的JS数组,那将是非常棒的。

1 个答案:

答案 0 :(得分:1)

让我们说你的文章是一个div。喜欢

<div class="article-content">{{ article.content }}</div>

现在我们可以将该div下的所有图像放入数组中。

$(document).ready(function(){

  // To save all images in an array named img_array
  var img_array = $('.article-content img').map(function() {
    return $(this).attr("src");
  });

  // You can run a for loop to display the images in the array.
  for (var i=0; i<img_array.length; i++) {
    alert(img_array[i]);
  }
});

如果有帮助,请告诉我。

参考:Get inside all images in array using jquery?