1

我正在使用 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 有问题...在这种情况下如何工作?

4

1 回答 1

2

所以你想让areas对象文字中的 for 循环填充属性?您可以使用立即调用的函数表达式 (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您的对象的属性。

于 2013-07-05T17:52:42.277 回答