在ImageMapster的区域中使用javascript FOR

时间:2013-07-05 17:42:54

标签: javascript imagemapster

我正在使用ImageMapster,我想选择定义到数组中的特定区域,如下所示:

var Carnet=new Array("6a","7a","8a","7c","8c","9c","23g","23g","14m","15m","16m");
var c = Carnet.length;

$('#central').mapster({
    mapKey: 'asiento',
    fillOpacity: 0,
    fillColor: "000000",
    stroke: true,
    strokeColor: "32CD32",
    strokeOpacity: 0.8,
    strokeWidth: 1,
    singleSelect : false,
    areas:  [
        for(i=0;i<c;i++)
        {
            {
                key: Carnets[i],
                fillOpacity: 0,
                fillColor: "000000",
                stroke: true,
                strokeColor: "00FF00",
                isSelectable:false
            }
    }]
});

但是FOR有问题...在这种情况下如何运作?

提前感谢。

1 个答案:

答案 0 :(得分:2)

所以你想让对象文字中的for循环填充属性areas?您可以使用立即调用的函数表达式(IIFE)轻松完成此操作:

$('#central').mapster({
    mapKey: 'asiento',
    fillOpacity: 0,
    fillColor: "000000",
    stroke: true,
    strokeColor: "32CD32",
    strokeOpacity: 0.8,
    strokeWidth: 1,
    singleSelect : false,
    areas:  (function(Carnets, c){
        var myArray = [];
        for(var i=0;i<c;i++)
        {
            myArray.push({
               key: Carnets[i],
               fillOpacity: 0,
               fillColor: "000000",
               stroke: true,
               strokeColor: "00FF00",
               isSelectable:false
            });
        }
        return myArray;
    })(Carnets, c)
});

现在,您的函数返回的数组将被分配给对象的areas属性。