jQuery 如何将数组传递给jquery中的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14727633/
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
how to pass an array to function in jquery
提问by Edward
I wish to pass an array to a function(can you tell me if im on the right track?)
我希望将一个数组传递给一个函数(你能告诉我我是否在正确的轨道上?)
and then in the function I wish to loop through the values in an array and append each one into the following LI element in HTML
然后在函数中我希望遍历数组中的值并将每个值附加到 HTML 中的以下 LI 元素中
This is what I have so far a user will code in the URL values he wants to pass:
这是我到目前为止用户将在他想要传递的 URL 值中编码的内容:
var arrValues = ['http://imgur.com/gallery/L4CmrUt', 'http://imgur.com/gallery/VQEsGHz'];
calculate_image(arrValues);
function calculate_image(arrValues) {
// Loop over each value in the array.
var jList = $('.thumb').find('href');
$.each(arrValues, function(intIndex, objValue) {
// Create a new LI HTML element out of the
// current value (in the iteration) and then
// add this value to the list.
jList.append($(+ objValue +));
});
}
}
HTML
HTML
<li>
<a class="thumb" href="" title="Title #13"><img src="" alt="Title #13" /></a>
<div class="caption">
<div class="download">
<a href="">Download Original</a>
</div>
<div class="image-title">Title #13</div>
<div class="image-desc">Description</div>
</div>
</li>
回答by turiyag
If you'd like to pass in an array, simply put it in as a parameter. In Javascript, you can pass numbers, strings, arrays, objects, and even functions in as parameters.
如果您想传入一个数组,只需将其作为参数放入即可。在 Javascript 中,您可以将数字、字符串、数组、对象甚至函数作为参数传递。
See this example for a thumbnail builder implementation: http://jsfiddle.net/turiyag/RxHys/9/
请参阅此示例以了解缩略图生成器实现:http: //jsfiddle.net/turiyag/RxHys/9/
First, define the arrays.
首先,定义数组。
var bluearray = [
'http://fc02.deviantart.net/fs30/f/2008/056/8/0/Purple_hair___Bipasha_Basu_by_mstrueblue.jpg',
'http://static.becomegorgeous.com/img/arts/2010/Feb/20/1805/purple_hair_color.jpg',
'http://img204.imageshack.us/img204/6916/celenapurpleqp7.jpg'
];
var greenarray = [
'http://25.media.tumblr.com/tumblr_m7fqmkNEhc1qlfspwo1_400.jpg',
'http://www.haircolorsideas.com/wp-content/uploads/2010/12/green-red-hair.jpg',
'http://fc02.deviantart.net/fs71/i/2010/011/9/c/Neon_Yellow_and_Green_Hair_by_CandyAcidHair.jpg'
];
Then when the DOM is loaded, call the functions to load the thumbnails.
然后在加载 DOM 时,调用函数来加载缩略图。
$(function() {
addThumbs(bluearray);
addThumbs2(greenarray);
});
addThumbs uses jQuery's each function to make things a bit cleaner. I find it looks better and is nicer to use that the normal Javascript for loop.
addThumbs 使用 jQuery 的 each 函数让事情变得更简洁。我发现它看起来更好,使用普通的 Javascript for 循环更好。
function addThumbs(paths) {
$.each(paths,function(index, value) {
$("div").append('<img src="' + value + '" />');
});
}
But if you're a fan of native Javascript, the normal for loop is implemented in addThumbs2
但是如果你是原生 Javascript 的粉丝,那么普通的 for 循环是在 addThumbs2 中实现的
function addThumbs2(paths) {
for(index in paths) {
$("div").append('<img src="' + paths[index] + '" />');
}
}