简体   繁体   English

单击添加类到传单js的路径

[英]Add class on click to a path with leaflet js

I am trying to add a class to the clicked polygon using: 我试图使用以下方法将类添加到单击的多边形中:

function addClass() {
    function style(feature) {
        return {
            className: "active"
        };
    }
}

function onEachFeature(feature, layer) {
    layer.on({
        click: addClass
    });
}

codepen 码笔

Docs here 文件在这里

The function app in your code only creates a function (and nothing more). 您代码中的function app仅创建一个函数(仅此而已)。 This function is only created, but never fired. 该函数仅创建,但从未触发。 Another problem is that this function does nothing that relevant to the element that was clicked. 另一个问题是此功能与被单击的元素无关。

Here is the change you are looking for: 这是您要查找的更改:

function app(e) {
    this.getElement().classList.add('active')
}

Here is a working codepen (based on your code): 这是一个有效的Codepen(根据您的代码):
http://codepen.io/anon/pen/pEmMRE http://codepen.io/anon/pen/pEmMRE

I added the active class to the CSS so you can actually see the change on the screen 我将active类添加到CSS中,因此您实际上可以在屏幕上看到更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM