jQuery - 获取 div 中图像的所有 src 并放入字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18101673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery - get all src of images in div and put into field
提问by Jaroslav Klim?ík
I want to modified this tutorialto 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 imageswhich 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.
我想根据我的要求修改本教程,但我有一个问题。我是 jQuery 的初学者,我想从特定的 c div 中获取所有图像源并将它们放入字段中。有一个变量图像,它是字段并包含一些图像,但我想要而不是从 div 获取所有图像源并将它们放入字段图像中。我知道这并不复杂,但我真的不知道该怎么做。
The source is here http://jsfiddle.net/s5V3V/36/
来源在这里http://jsfiddle.net/s5V3V/36/
This is the variable imagefrom source on jsfiddle which I want fill from div instead what I have there now:
这是来自 jsfiddle 源的可变图像,我想从 div 填充它,而不是我现在拥有的图像:
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.
先谢谢了。
回答by Arun P Johny
In dom ready try
在 dom 中准备好尝试
var images = $('.thumbnailArrows').children('img').map(function(){
return $(this).attr('src')
}).get()
回答by André Dion
Assuming by "field" you mean variable or Array:
假设“字段”是指变量或数组:
var images = $('#imageHolder').find('img').map(function() { return this.src; }).get();
回答by knikolov
You can just loop with .each() and get the attribute
您可以使用 .each() 循环并获取属性
(function(){
var images = [];
$("#imageHolder img").each(function(){
images.push($(this).attr('src'))
})
console.log(images);
})()
回答by Igor Monteiro
find fix your problem :
找到解决您的问题:
var images = $(".thumbnailArrows").find('img').map(function () {
return $(this).attr('src')
}).get()
回答by nnnnnn
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:
如果您说要创建一个变量images
,该变量将是一个数组,其中填充了div 中src
所有 img 元素的URL,thumbnails
那么您可以执行以下操作:
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"
.)
(如果我选择了错误的 div 作为容器,显然你可以通过用"#thumbnails"
选择器替换正确的 div 来纠正它,也许"#imageHolder"
。)
Note that your use of the term "field" is incorrect. You seem to mean "array", or possibly just "variable".
请注意,您对“字段”一词的使用是不正确的。您的意思似乎是“数组”,或者可能只是“变量”。