首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加/删除函数缩短代码

添加/删除函数缩短代码
EN

Code Review用户
提问于 2013-04-25 14:15:46
回答 1查看 4.5K关注 0票数 2

我正在通过以下途径实现每个大陆的悬停状态:

代码语言:javascript
复制
var continentId =""
function getID(continentId){
    jQuery.each(mapObject.mapData.paths, function(i, val) {
            if (val.continent == continentId){
                continentCodes[i] = "#3e9d01";
                mapObject.series.regions[0].setValues(continentCodes);
            }
    });
}

function removeGetID(continentId){
    jQuery.each(mapObject.mapData.paths, function(i, val) {
            if (val.continent == continentId){
                continentCodes[i] = "#128da7";
                mapObject.series.regions[0].setValues(continentCodes);
            }
    });
}

//LIST COUNTRIES & CONTINENTS TEMP
jQuery('.continentLink').hover(function(e) {
    continentId = this.id;
    getID(continentId);
}, function(){
    removeGetID(continentId);
});

有没有办法缩短这个时间,这样我就不必有多个each语句了?我真的在努力学习如何编写高效的代码。

下面是完整的代码,如果有帮助的话:JSFIDDLE

代码语言:javascript
复制
jQuery(function(){
//JSON MARKERS
var markers = [{latLng: [-34.033333300000000000, 23.066666700000040000], name: 'Knysna', info:'its got a lake...'},
    {latLng: [-33.924868500000000000, 18.424055299999963000], name: 'Cape Town', info:'its nice...'}];
//JSON MARKERS  

//JSON STYLING
var markerStyle = {initial: {fill: '#F8E23B',stroke: '#383f47'}};
var regionStyling = {initial: {fill: '#128da7'},hover: {fill: "#A0D1DC"}};
//JSON STYLING

//GLOBAL VARIABLES
var countryList = "", continentList = "";
var resultsDup = {};
var continentCodes = {};
//GLOBAL VARIABLES

//INIT MAP PLUGIN
jQuery('#world-map').vectorMap({
    map: 'world_mill_en',
    normalizeFunction: 'polynomial',
    markerStyle:markerStyle,
    regionStyle:regionStyling,
    backgroundColor: '#383f47',
    series: {regions: [{values: {},attribute: 'fill'}]},
    markers: markers,
    onRegionClick:function (event, code){
        jQuery('#world-map').vectorMap('set', 'focus', code);
    },
    onMarkerClick: function(events, index){
        jQuery('#infobox').html(markers[index].name);
    }
});
//INIT MAP PLUGIN

var mapObject  = jQuery('#world-map').vectorMap('get', 'mapObject');

//LIST COUNTRIES & CONTINENTS
jQuery.each(mapObject.mapData.paths, function(i, val) {

    countryList += '<li><a id='+i+' class="countryLink">'+val.name+'</a></li>';

    //remove duplicate continents
    var resultsList = val.continent;
    if (resultsDup[resultsList]) {
        jQuery(this).remove();
    }else{
        resultsDup[resultsList] = true;
        continentList += '<li><a id='+val.continent+' class="continentLink">'+val.continent+'</a></li>';
    }
    //remove duplicate continents


});
//display countries
jQuery('#countryList').html(countryList);

//display continents
jQuery('#continentList').html(continentList);

var continentId =""
function getID(continentId){
    jQuery.each(mapObject.mapData.paths, function(i, val) {
            if (val.continent == continentId){
                continentCodes[i] = "#3e9d01";
                mapObject.series.regions[0].setValues(continentCodes);
            }
    });
}

function removeGetID(continentId){
    jQuery.each(mapObject.mapData.paths, function(i, val) {
            if (val.continent == continentId){
                continentCodes[i] = "#128da7";
                mapObject.series.regions[0].setValues(continentCodes);
            }
    });
}

//LIST COUNTRIES & CONTINENTS TEMP
jQuery('.continentLink').hover(function(e) {
    continentId = this.id;
    getID(continentId);
}, function(){
    removeGetID(continentId);
});

//Zoom to Country Function
jQuery('.countryLink').click(function(e) {
    jQuery('#world-map').vectorMap('set', 'focus', this.id);
});

//Continent Hover function

});
EN

回答 1

Code Review用户

回答已采纳

发布于 2014-01-14 16:21:03

您正在寻找删除副本粘贴代码,以使它更干燥。

除此之外,无论您是getID还是removeGetID,都需要设置一些ID,然后更新mapObject。此外,您似乎不需要在循环中更新mapObject,这很慢。

我会反悔

代码语言:javascript
复制
var helpfullyNamedConstant1 = '#3e9d01';
var helpfullyNamedConstant2 = '#128da7';

function setContinentCodes(continentId, continentCodesValue ){
    jQuery.each(mapObject.mapData.paths, function(i, val) {
            if (val.continent == continentId){
                continentCodes[i] = continentCodesValue ;
            }
    });
    mapObject.series.regions[0].setValues(continentCodes);
}

jQuery('.continentLink').hover(function(e) {
    setContinentCodes(this.id, helpfullyNamedConstant1);
}, function(){
    setContinentCodes(this.id, helpfullyNamedConstant2);
});
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/25493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档