对于数组中的每个项目,如果匹配,则使用jvectormaps执行此操作

时间:2012-08-01 18:23:18

标签: javascript jquery arrays variables jvectormap

我正在尝试设置一个状态数组,如果数组中存在状态或值,则执行一个函数。下面是我想要修改的实际代码,“stateRedirects”var就是我在if(code === stateRedirects){}中尝试的内容...

var stateRedirects = [
    'US-TX',
    'US-CA',
    'US-AL'
    // more will be added
]

$('.map').vectorMap({
    map: 'us_en',
    backgroundColor: 'transparent',
    color: '#0071A4',
    hoverColor: false,
    hoverOpacity: 0.5,
    normalizeFunction: 'polynominal',
    scaleColors: ['#C8EEFF', '#0071A4'],
    onLabelShow: function(event, label, code){
        if (code === stateRedirects ) {
            //do nothing
        }
        else if (code) { //if a state is not specified in var stateRedirects then prevent default
            event.preventDefault();
        }                   
    },      
    onRegionOver: function(event, code){
        if (code === stateRedirects ) {
            //do nothing
        }
        else if (code) { //if a state is not specified in var stateRedirects then prevent default
            event.preventDefault();
        }                   
    },
    onRegionClick: function (event, code) {
        window.location = '/' + code;
    }   
});  

根据评论,我将其更改为使用[和],但无法弄清楚如何让它看看代码是否与此行中的数组匹配if (code === stateRedirects ) {

1 个答案:

答案 0 :(得分:1)

使用方括号正确填写{p} Define your array州代码。然后使用array.contains functionality,这似乎就是你想要的。