Add class on svg path element

前端 未结 1 508
你的背包
你的背包 2021-01-04 21:34

In my project I have an SVG world map with different paths with different id\'s and one class of map-path. For each country click I want to add class on each pa

1条回答
  •  再見小時候
    2021-01-04 22:21

    JQuery function addClass() will not work here and can't add a class to an SVG.

    Use .attr() instead :

    $('body').on('click','.map-path',function() {
        $(this).attr("class", "map-path newclass");
    });
    

    You could use pure js solution with setAttribute() method :

    $('body').on('click','.map-path',function(e) {
        e.target.setAttribute("class", "map-path newclass");
    });
    

    Or use classList.add() that works in modern browsers :

    $('body').on('click','.map-path',function(e) {
        e.target.classList.add('newclass');
    });
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题