【发布时间】:2017-05-01 18:20:45
【问题描述】:
好的,我有一个问题,也许是一个简单的问题,我是新手。
我有一个 SVG 图像,它实际上是一张地图(区域地图),分为多个部门(即城市)。没关系,SVG 完美运行。另外,我有一个简单的下拉列表(进入HTML)。
这就是我想要的:
当有人在菜单中选择一个选项(城市)时,选择器(地区)会显示为选中状态。而且,当有人选择选择器(地区)时,菜单(城市)中的选项会显示为选中状态。
我有附加图片。
非常感谢。
更新:
下拉菜单 HTML 代码:
<label for="sel1">Seleziona Area:</label>
<select class="form-control" id="sel1">
<option>1 - Udine Centro</option>
<option>2 - Rizzi / S. Domenico / Cormor / S. Rocco</option>
<option>3 - Laipacco / San Gottardo</option>
<option>4 - Udine sud</option>
<option>5 - Cussignacco</option>
<option>6 - S. Paolo / S. Osvaldo</option>
<option>7 - Chiavris / Paderno</option>
</select>
SVG 图像的 Javascript 代码:
$(document).ready(function() {
$('g.chiavris').click(function() {
$('g.chiavris').fadeOut('slow');
});
$("g.region").hover(function() {
//mouse over
$(this).find('.map-image').css('fill', '#8B8B8B');
$(this).find('.map-title').css('display', 'block');
}, function() {
//mouse out
$(this).find('.map-image').css('fill', '#ccc');
$(this).find('.map-title').css('display', 'none');
});
$('.region').click(function(event) {
var regions = $('.region');
console.log(regions);
for(var i=0; i<regions.length; i++){
console.log('tutti messi normali '+ i);
$(regions[i]).find('.map-image').css('fill', '#ccc');
$(regions[i]).find('.map-title').css('display', 'none');
$(regions[i]).bind('mouseenter mouseleave');
}
//DOPO
$(this).find('.map-image').css('fill', '#FF7409');
$(this).find('.map-title').css('display', 'block');
$(this).unbind('mouseenter mouseleave');
});
});
更新 2:
好的,谢谢大家,我已经升级了你的代码:
// per selezionare i "polygon" che influisce sul select
$(".map-image").on('click', function(evt) {
// Get the id of the region we clicked on
var regionId = evt.target.id;
// Update the dropdown
$('#sel1 option').removeAttr('selected')
.filter('[value=' + regionId + ']')
.attr('selected', true);
// Highlight the relevant region
setRegion(regionId);
});
// Per selezionare dal select e avere il colore nella mappa
$("#sel1").change(function(evt){
//console.log($(this).val());
var name_region = ($(this).val());
var regions = $(document).find('#'+ name_region).get(0);
//console.log(regions);
$(regions).css('fill', '#FF7409');
});
/*function onRectClick(evt)
{
// Get the id of the region we clicked on
var regionId = evt.target.id;
// Update the dropdown
$("#sel1").val(regionId).change();
// Highlight the relevant region
setRegion(regionId);
}*/
function onSelectChange() {
// Get selected class from dropdown
var selectedRegion = $("#sel1").val();
// Highlight the relevant region
setRegion(selectedRegion);
}
function setRegion(regionId) {
// Remove "selected" class from current region
$("polygon.selected").removeClass("selected");
// Add "selected" class to new region
$('polygon#' + regionId).addClass("selected");
// Note: for addClass() etc to work on SVGs, you need jQuery 3+
}
// Init map based on default select value
onSelectChange();
【问题讨论】:
-
到目前为止你尝试过什么?你能显示一些代码吗?
-
我更新了我的帖子。您可以检查代码。我无法附上 SVG 代码,因为它对于帖子来说太长了 :( 如果你需要它,请告诉我如何附上它。谢谢。如果你需要更多信息问我,谢谢!!
标签: javascript jquery html svg