Javascript 如何将图像的值获取到对象/数组中
我试图创建一个函数,它在其中循环通过一组图像,获取图像的值并将其添加到对象/数组中Javascript 如何将图像的值获取到对象/数组中,javascript,jquery,arrays,Javascript,Jquery,Arrays,我试图创建一个函数,它在其中循环通过一组图像,获取图像的值并将其添加到对象/数组中 <div id="col1"> <img id="layer_a" src="imga.jpg" height="320" width="400" class="holder"> <img id="layer_b" src="imgb.jpg" height="320" width="400" class="holder"> <img id="la
<div id="col1">
<img id="layer_a" src="imga.jpg" height="320" width="400" class="holder">
<img id="layer_b" src="imgb.jpg" height="320" width="400" class="holder">
<img id="layer_c" src="imgc.jpg" height="320" width="400" class="holder">
<img id="layer_d" src="imgd.jpg" height="320" width="400" class="last">
</div>
<div id="col2">
<img id="layer_e" src="imge.jpg" height="320" width="400" class="holder">
<img id="layer_f" src="imgf.jpg" height="320" width="400" class="last">
</div>
如此类推,在没有两次获得相同id的情况下,我认为@am not I am是正确的,随后会有呼叫。如果您需要这些调用,我有一个简单的(JSFIDLE)[http://jsfiddle.net/CypAA/]这就够了
var data = [];
var ids = {};
function pickImage() {
$('img.holder').each(function() {
if (ids[this.id]) return;
ids[this.id] = 1;
data.push({
id: this.id,
src: this.src,
width: this.width,
height: this.height
});
});
}
pickImage();
pickImage();
pickImage();
console.log(data);
我认为@am not I am是对的,后面还会有电话。如果您需要这些调用,我有一个简单的(JSFIDLE)[http://jsfiddle.net/CypAA/]这就够了
var data = [];
var ids = {};
function pickImage() {
$('img.holder').each(function() {
if (ids[this.id]) return;
ids[this.id] = 1;
data.push({
id: this.id,
src: this.src,
width: this.width,
height: this.height
});
});
}
pickImage();
pickImage();
pickImage();
console.log(data);
您必须将JS修改为
var data = {
images: []
};
function pickimage() {
$("img.holder").each(function() {
var _self = $(this);
data.images.push({
'id': _self.attr('id'),
'src': _self.attr('src'),
'width': _self.attr('width'),
'height': _self.attr('height')
});
});
}
pickimage();
for (var i = 0, len=data.images.length; i < len; i++) {
console.log(data.images[i]);
}
var数据={
图片:[]
};
函数pickimage(){
$(“img.holder”)。每个(函数(){
var _self=$(本);
data.images.push({
'id':_self.attr('id'),
'src':_self.attr('src'),
'width':_self.attr('width'),
‘高度’:_self.attr(‘高度’)
});
});
}
pickimage();
对于(变量i=0,len=data.images.length;i
请参见您必须将JS修改为
var data = {
images: []
};
function pickimage() {
$("img.holder").each(function() {
var _self = $(this);
data.images.push({
'id': _self.attr('id'),
'src': _self.attr('src'),
'width': _self.attr('width'),
'height': _self.attr('height')
});
});
}
pickimage();
for (var i = 0, len=data.images.length; i < len; i++) {
console.log(data.images[i]);
}
var数据={
图片:[]
};
函数pickimage(){
$(“img.holder”)。每个(函数(){
var _self=$(本);
data.images.push({
'id':_self.attr('id'),
'src':_self.attr('src'),
'width':_self.attr('width'),
‘高度’:_self.attr(‘高度’)
});
});
}
pickimage();
对于(变量i=0,len=data.images.length;i
请参见
当我在拉小提琴时,你得到了几个类似的答案。选择最适合你的
JS
当我在拉小提琴时,你得到了几个类似的答案。选择最适合你的
JS
你能改变数据结构吗?如果使用ID作为键来存储图像属性,而不是将它们存储在数组中,则会简单一些。仅供参考,
$(“img.holder”)
应删除空格$(“img.holder”)
和此.attr(…)
将失败。是否向数组添加具有唯一id的图像?你是如何在数据中多次获得相同的id的?@Charles:我想这是从随后调用pickimage()
得到的。不确定。你能改变数据结构吗?如果使用ID作为键来存储图像属性,而不是将它们存储在数组中,则会简单一些。仅供参考,$(“img.holder”)
应删除空格$(“img.holder”)
和此.attr(…)
将失败。是否向数组添加具有唯一id的图像?你是如何在数据中多次获得相同的id的?@Charles:我想这是从随后调用pickimage()
得到的。不过我不确定。
var data = {
'images': Array()
}
function pickimage() {
$("#imagecontainer").find('img').each(function() {
var img= $(this)
var idset = img.attr('id');
var srcset = img.attr('src');
var heightset = img.attr('height');
var widthset = img.attr('width');
var newSet = {
'id': idset,
'src': srcset,
'width': widthset,
'height': heightset
};
data.images.push(newSet);
});
}
pickimage();
alert(JSON.stringify(data))