programmatically trigger click event in svg rect element

前端 未结 2 1626
广开言路
广开言路 2021-01-03 04:45

How to programmatically trigger click event in svg rectangle element element? like :$(\"#targetElm\").triger(\"click\");

     

        
相关标签:
2条回答
  • 2021-01-03 05:13

    You can use Highcharts' internal function called firePointEvent for this:

    var chart = Highcharts.chart('container', {
    
      series: [{
        type: 'column',
        data: [1],
        point: {
            events: {
            click: function() {
                console.log('Click event fired');
            }
          }
        }
      }]
    });
    
    chart.series[0].points[0].firePointEvent('click');
    

    Live demo: http://jsfiddle.net/BlackLabel/xbkwqzc5/

    0 讨论(0)
  • 2021-01-03 05:14

    How about this

    document.getElementById("targetElm").dispatchEvent(new Event('click'));
    

    Here's a working example...

    document.getElementById("targetElm").dispatchEvent(new Event('click'));
    <svg version="1.1" class="highcharts-root" style="font-family:MontserratRegular;font-size:12px;" xmlns="http://www.w3.org/2000/svg" width="441" height="319.5" viewBox="0 0 441 319.5">
        <rect id="targetElm" onclick="alert('hi')"/>
    </svg>

    i.e. create a click event and dispatch it to the element. Although you don't seem to have any handler for the click event in your question.

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