我是javacript的新手,我正在尝试将一个数字计数器作为循环添加到下面的函数中,以免我输入8次!
函数需要调用loadPopup1 - loadPopup8,#toPopup div需要是toPopup1 - toPopup8。
这是我的代码:
function loadPopup() {
if(popupStatus == 0) { // if value is 0, show popup
closeloading(); // fadeout loading
$("#toPopup").fadeIn(0500); // fadein popup div
$("#backgroundPopup").css("opacity", "0.7"); // css opacity, supports IE7, IE8
$("#backgroundPopup").fadeIn(0001);
popupStatus = 1; // and set value to 1
}
}
非常感谢你的帮助!
皮特
答案 0 :(得分:2)
将参数移交给函数...
function loadPopup(index) {
...
$("#toPopup" + index).fadeIn(0500);
...
}
然后,如果您想对#1和#7采取行动,请将其称为传递索引......
loadPopup(1);
loadPopup(7);
答案 1 :(得分:0)
此代码将创建您的8个函数:
for (var i = 1; i <= 8; ++i) {
(function(i) {
window['loadPopup' + i] = function loadPopup() {
if(popupStatus == 0) { // if value is 0, show popup
closeloading(); // fadeout loading
$("#toPopup" + i).fadeIn(0500); // fadein popup div
$("#backgroundPopup").css("opacity", "0.7"); // css opacity, supports IE7, IE8
$("#backgroundPopup").fadeIn(0001);
popupStatus = 1; // and set value to 1
}
};
})(i);
}
答案 2 :(得分:0)
function loadPopup(i) {
if(popupStatus == 0) { // if value is 0, show popup
closeloading(); // fadeout loading
$("#toPopup" + i).fadeIn(0500); // fadein popup div
$("#backgroundPopup").css("opacity", "0.7"); // css opacity, supports IE7, IE8
$("#backgroundPopup").fadeIn(0001);
popupStatus = 1; // and set value to 1
}
}
var wrapper={};
for (var i =0; i < 8; i++){
wrapper['loadPopup' + i] = new function(){loadPopup(i);};
}
// so you can call
wrapper.loadPopup3();