0

我对 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
    }   
}

非常感谢您的帮助!

皮特

4

3 回答 3

2

将参数交给函数...

function loadPopup(index) {
   ...
   $("#toPopup" + index).fadeIn(0500);
   ...
}

然后,如果您想对 #1 和 #7 采取行动,则将其称为传递索引...

loadPopup(1);
loadPopup(7);
于 2013-06-26T09:09:55.263 回答
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);
}
于 2013-06-26T09:10:27.423 回答
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();
于 2013-06-26T09:20:28.300 回答