jQuery - get all src of images in div and put into field

▼魔方 西西 提交于 2019-12-30 06:57:10

问题


I want to modified this tutorial to my requirements but there is one problem to me. I'm a beginner with jQuery and I would like to get all image sources from specifïc div and put them into field. There is a variable images which is field and contain some images but I want instead of this get all image sources from div and put them into field images. I know that isn't such a complicated but I don't really know how to do it.

The source is here http://jsfiddle.net/s5V3V/36/

This is the variable image from source on jsfiddle which I want fill from div instead what I have there now:

images = ['http://kimjoyfox.com/blog/wp-content/uploads/drwho8.jpg',
    'http://kimjoyfox.com/blog/wp-content/uploads/drwho7.jpg',
    'http://kimjoyfox.com/blog/wp-content/uploads/drwho6.jpg',
    'http://kimjoyfox.com/blog/wp-content/uploads/drwho5.jpg',
    'http://kimjoyfox.com/blog/wp-content/uploads/drwho4.jpg',
    'http://kimjoyfox.com/blog/wp-content/uploads/drwho3.jpg',
    'http://kimjoyfox.com/blog/wp-content/uploads/dr-whos-tardis.png',
    'http://kimjoyfox.com/blog/wp-content/uploads/drwho9.jpg',
    'http://kimjoyfox.com/blog/wp-content/uploads/drwho1.jpg'];

Thanks advance.


回答1:


In dom ready try

var images = $('.thumbnailArrows').children('img').map(function(){
    return $(this).attr('src')
}).get()



回答2:


Assuming by "field" you mean variable or Array:

var images = $('#imageHolder').find('img').map(function() { return this.src; }).get();



回答3:


You can just loop with .each() and get the attribute

(function(){
    var images = [];
    $("#imageHolder img").each(function(){
      images.push($(this).attr('src'))
    })
    console.log(images);
  })()

http://jsbin.com/ogekos/1/edit




回答4:


If you're saying you want to create a variable images that will be an array populated with the src URLs from all the img elements in your thumbnails div then you can do this:

var images = $("#thumbnails").find("img").map(function() { return this.src; }).get();

(If I've picked the wrong div as the container obviously you can correct that by replacing "#thumbnails" with a selector for whatever the right div is, perhaps "#imageHolder".)

Note that your use of the term "field" is incorrect. You seem to mean "array", or possibly just "variable".




回答5:


find fix your problem :

   var images = $(".thumbnailArrows").find('img').map(function () {
                                                            return $(this).attr('src')
                                                        }).get()


来源:https://stackoverflow.com/questions/18101673/jquery-get-all-src-of-images-in-div-and-put-into-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!