如何更改谷歌地图中控件div中的按钮样式



如何通过点击地图控件div上的按钮来获取元素并更改样式?我喜欢在地图上有同样的效果,就像在表中的按钮一样。

代码:jsFiddle

更新:

在您的帮助下,我得到了预期的结果:jsFiddle2

我已经在这里更新了你的gMap.select,它正在运行,希望这是你想要的。

select: function (buttonId){
    gMap.buttons.$line.className="unselected";
    gMap.buttons.$placemark.className="unselected";
    document.getElementById(buttonId).className="selected";
    var divs=window.parent.frames[0].document.getElementsByTagName('div');
    for(i=0;i<divs.length;i++)
    {
        if(divs[i].id.charAt(0)=="c") divs[i].className="unselected";
    }
    var btn='c_'+buttonId;
    window.parent.frames[0].document.getElementById(btn).className="selected";
}

更新:这是更新后的小提琴。希望你想要这个。

更新被标记为//在小提琴中更新

这是如何在谷歌地图(使用Javascript)中添加自定义控件及其事件处理程序的另一个例子

var map;
function initialize() {
map = new google.maps.Map(document.getElementById('map'));
map.setCenter(new google.maps.LatLng(48.77565,9.181802));
map.setZoom(12);
map.setMapTypeId( google.maps.MapTypeId.ROADMAP );
var controlDiv = document.createElement('div');
var table=document.createElement('TABLE');
var tbdy=document.createElement('TBODY');
var tr=document.createElement('TR');
var btn1=document.createElement('input');
btn1.setAttribute("type", "button");
btn1.id="c_placemark_b";
btn1.setAttribute("value", "Button One");
var td1=document.createElement('TD');
td1.appendChild(btn1);
var btn2=document.createElement('input');
btn2.setAttribute("type", "button");
btn2.id="c_line_b";
btn2.setAttribute("value", "Button Two");
var td2=document.createElement('TD');
td2.appendChild(btn2);
tr.appendChild(td1);
tr.appendChild(td2);
tbdy.appendChild(tr);
table.appendChild(tbdy);
controlDiv.appendChild(table);
google.maps.event.addDomListener(btn1, 'click', function() {
    //DoSomething
    alert(this.id);
});
google.maps.event.addDomListener(btn2, 'click', function() {
    //DoSomething
    alert(this.id);
});
controlDiv.index = 1;
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(controlDiv);
}

最新更新