我想根据我的要求修改本教程,但有一个问题。我是jQuery的初学者,我想从specifïcdiv获得所有图像源,并将它们放入字段。有一个变量images,它是字段,包含一些图像,但我希望从div中获取所有图像源,并将它们放入字段images中。我知道这并不复杂,但我真的不知道该怎么做
消息来源在这里http://jsfiddle.net/s5V3V/36/
这是jsfiddle上源代码中的变量image,我想从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'];
提前感谢。
在dom中尝试
var images = $('.thumbnailArrows').children('img').map(function(){
return $(this).attr('src')
}).get()
假设"字段"是指变量或数组:
var images = $('#imageHolder').find('img').map(function() { return this.src; }).get();
您可以使用.each()循环并获得属性
(function(){
var images = [];
$("#imageHolder img").each(function(){
images.push($(this).attr('src'))
})
console.log(images);
})()
http://jsbin.com/ogekos/1/edit
如果您想创建一个变量images
,它将是一个数组,由thumbnails
div中所有img元素的src
URL填充,那么您可以执行以下操作:
var images = $("#thumbnails").find("img").map(function() { return this.src; }).get();
(如果我选择了错误的div作为容器,那么很明显,您可以通过将"#thumbnails"
替换为正确div的选择器来纠正这一问题,也许是"#imageHolder"
。)
请注意,您使用的术语"字段"是不正确的。你的意思似乎是"数组",或者可能只是"变量"。
find修复您的问题:
var images = $(".thumbnailArrows").find('img').map(function () {
return $(this).attr('src')
}).get()