返回数组x项的同时 [英] Return x items of an array at a time
问题描述
好日子所有。
我这个小jQuery的code。
I have this little jquery code.
$(document).ready(function() {
$("#mains").one("click", function() {
var pmmain = ["aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo", "ppp"];
var a = 0;
var b = 0;
for (a = 0; a < pmmain.length; a++) {
$("#pm-page-main").append("<div class=\"main-box\"><div class=\"title-box\"><span class=\"reg-wht-bold\">" + pmmain[a] + "</span></div><BR><BR><img src=\"imgs\\" + pmmain[a] + ".png\"></div>");
}
});
});
在code是工作,但我真正想要做的就是回到我们说4个项目在一个时间。我还想做很多事情,但我猜要求它一块一块可以让我更好地了解它不是要求所有一气呵成。
The code is working but what I really want to do is to return let's say 4 items at a time. I still want to do a lot of things with it but I guess asking it piece by piece would make me understand it better than asking it all in one go.
更新:
我不知道,如果有人有兴趣,但是这是我结束了
I don't know if anyone is interested but this is what I end up with
$(document) .ready(function(){
var a;
var b;
var c;
var d;
var pmmain =["Citrix", "Coach", "Disney", "Edison", "Eversource", "Fedex", "General Dynamics", "Hertz", "Kimco", "Nabors", "Starbucks", "Timewarner", "TW Telecom", "Weatherford", "Western Union", "Zoetis"];
$("#mains") .one("click",function(){
c=0;
d=c+4;
a=4;
b=0;
for (a=c; a<d; a++){
$("#pm-page-main") .append("<div class=\"main-box\"><div class=\"title-box\"><span class=\"reg-wht-bold\">"+pmmain[a]+"</span></div><BR><BR><img src=\"imgs\\"+pmmain[a]+".png\"></div>");
}
});
$("#next-four").click(function() {
b = a
d = b +4;
$("#pm-page-main") .html("");
for (b=a; b < d; b++) {
$("#pm-page-main") .append("<div class=\"main-box\"><div class=\"title-box\"><span class=\"reg-wht-bold\">" + pmmain[b] + "</span></div><BR><BR><img src=\"imgs\\" + pmmain[b] + ".png\"></div>").hide().show();;
}
a = a+4;
});
});
我决定去这一点,因为它给了我明确的股利,并把在接下来的四个选项。
I decided to go with this because it gives me the option to clear the div and put in the next four.
我想为我的下一步是要弄清楚如何从何时数组结束去阻止它。
I guess the next step for me is to figure out how to stop it from going when the array ends.
感谢所有帮助!
推荐答案
的简单改变你的code:
One simple change to your code:
$(document).ready(function() {
$("#mains").one("click", function() {
var pmmain = ["aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo", "ppp"];
var a = 0;
var b = 0;
for (a = 0; a < 4; a++) { //change the value of a to be the limit
$("#pm-page-main").append("<div class=\"main-box\"><div class=\"title-box\"><span class=\"reg-wht-bold\">" + pmmain[a] + "</span></div><BR><BR><img src=\"imgs\\" + pmmain[a] + ".png\"></div>");
}
});
});
小提琴这里: http://jsfiddle.net/60mav6ev/2/
编辑(按要求):
要获得下一组在点击按钮四个项目,你需要的东西是这样的:
To get the next set of four items upon button click, you need something like this:
$(document).ready(function() {
var a;
var b;
var y;
var z;
var pmmain = ["aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo", "ppp"];
$("#mains").one("click", function() {
y = 0;
z = y +4;
a = 4;
b = 0;
for ( a=y; a < z; a++) {
$("#pm-page-main").append("<div class=\"main-box\"><div class=\"title-box\"><span class=\"reg-wht-bold\">" + pmmain[a] + "</span></div><BR><BR><img src=\"imgs\\" + pmmain[a] + ".png\"></div>");
}
});
$("#fourmore").click(function() {
b= a
z = b +4;
for (b=a; b < z; b++) {
$("#pm-page-main").append("<div class=\"main-box\"><div class=\"title-box\"><span class=\"reg-wht-bold\">" + pmmain[b] + "</span></div><BR><BR><img src=\"imgs\\" + pmmain[b] + ".png\"></div>");
}
a = a+4;
});
});
小提琴: http://jsfiddle.net/60mav6ev/5/
这篇关于返回数组x项的同时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!