【问题标题】:Trying to create images with an array of image src attributes using jquery尝试使用 jquery 创建具有图像 src 属性数组的图像
【发布时间】:2014-10-09 13:55:48
【问题描述】:

我有一个来自图像的 imag src 属性数组,我可以将它们推送到数组中:

var imgSrcArray = Array();
$('.ito-image').each(function(){
     imgSrcArray.push($(this).attr('src'));
});

我现在想使用这些属性并在我的网页上为不同的目的创建新图像,我该怎么做。这是他的最佳做法吗?

【问题讨论】:

  • 您是否考虑过将图像复制/移动到您希望它们附加的任何新位置?
  • 你有什么尝试吗?

标签: jquery arrays image


【解决方案1】:

这就是你想要的吗?

for (var i=0; i < imgSrcArray.length; i++){

  $('body').append("<img src='"+ imgSrcArray[i] +"' />");

}

【讨论】:

    【解决方案2】:

    您可以使用 $.each() 以相同的方式迭代并附加到某个容器 div:

    $(imgSrcArray).each(function (index, item) {
    
        $("#new").append('<img src="' + item + '"/>');
    
    });
    

    或者你可以直接做:

    $('.ito-image').each(function(){
         $("#new").append('<img src="' + $(this).attr("src") + '"/>');
    });
    

    小提琴:

    http://jsfiddle.net/ehsansajjad465/0oh6mma0/

    【讨论】:

      猜你喜欢
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 2013-11-30
      • 1970-01-01
      • 2019-10-17
      • 2012-02-27
      相关资源
      最近更新 更多