html按钮不会改变颜色

时间:2014-10-01 11:38:35

标签: javascript jquery html leaflet

我正在使用liflet API。 我在地图上创建标记。 当按下标记时弹出鞋面。 在弹出窗口中有两个按钮。 我想要做的是让按钮在按下时改变颜色。 问题是当我尝试在弹出窗口打开时重置颜色 只有当我第二次打开弹出窗口时才会产生影响。 我检查了弹出事件和点击事件的工作。 按钮创建代码:

if(!marker.getPopup()){
        var tooltipContent = $('<div />');
        $.each(marker._myId, function(index,value){
            var current = $("<button id=\"router-"+value.properties.name+"\" type=\"button\" style=\"width:100%;color:black\" class='tooltipName' >"+value.properties.name+"</button>")
                .click(function(){
                    handleClickOnSwitch(value);
                    var button=$("#router-"+value.properties.name);
                    button.css("background-color","grey");

                });
            tooltipContent=tooltipContent.append(current);
        });
        marker.bindPopup(tooltipContent[0],{'closeButton' : false,}).openPopup();
    }

单击我将按钮coller更改为灰色。 每次打开弹出窗口时我都会打电话:

function resetButtonPress(marker){
    $.each(marker._myId, function(index,router){
        var button=$("#router-"+router.properties.name);
        button.css("background-color","RGB(221,221,221)");
    });
}

我将不胜感激。

1 个答案:

答案 0 :(得分:1)

最终问题的根本原因是竞争条件。 我试图更新的按钮元素创建得不够快,结果也是如此 颜色变化没有生效。 我的解决方案是每次打开时在弹出窗口中创建按钮。 这样我可以规避问题。

相关问题