本文介绍了jQuery的获得名单IMG源属性和推入阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的缩略图列表,并希望图像路径(源)推入一个数组:tn_array
< UL ID =缩略图>
<立GT;< IMG SRC =somepath / TN / 004.jpgALT =第四标题/>< / A>< /李>
<立GT;< IMG SRC =somepath / TN / 005.JPGALT =第五标题/>< / A>< /李>
<立GT;< IMG SRC =somepath / TN / 006.jpgALT =第六标题/>< / A>< /李>
< / UL>
解决方案
您可以创建SRC属性数组更直接使用:
VAR tn_array = $(#缩略图IMG)。图(函数(){
返回$(本).attr(SRC);
});
编辑: tn_array
这里是一个对象,而不是严格的Javascript数组,但它将作为一个数组。例如,这是合法的code:
的for(int i = 0; I< tn_array.length;我++){
警报(tn_array [I]);
}
您可以拨打不过,这将使它严格的数组:
VAR tn_array = $(#缩略图IMG)。图(函数(){
返回$(本).attr(SRC);
})。得到();
你如何分辨?呼叫:
警报(obj.constructor.toString());
第一个版本将是:
函数对象(){[本地code]}
第二:
功能阵列(){[本地code]}
I have this thumbnail list and would like push the image paths (sources) into an array: tn_array
<ul id="thumbnails">
<li><img src="somepath/tn/004.jpg" alt="fourth caption" /></a></li>
<li><img src="somepath/tn/005.jpg" alt="fifth caption" /></a></li>
<li><img src="somepath/tn/006.jpg" alt="sixth caption" /></a></li>
</ul>
解决方案
You can create the array of src attributes more directly using map()
:
var tn_array = $("#thumbnails img").map(function() {
return $(this).attr("src");
});
Edit: tn_array
is an object here rather than a strict Javascript array but it will act as an array. For example, this is legal code:
for (int i=0; i<tn_array.length; i++) {
alert(tn_array[i]);
}
You can however call get()
, which will make it a strict array:
var tn_array = $("#thumbnails img").map(function() {
return $(this).attr("src");
}).get();
How do you tell the difference? Call:
alert(obj.constructor.toString());
The first version will be:
function Object() { [native code] }
The second:
function Array() { [native code] }
这篇关于jQuery的获得名单IMG源属性和推入阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!