本文介绍了以编程方式触发 svg 矩形元素中的点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以编程方式触发 svg 矩形元素元素中的点击事件?像 :$("#targetElm").triger("click");
How to programmatically trigger click event in svg rectangle element element?like :$("#targetElm").triger("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">
<desc>Created with Highcharts 6.0.1</desc>
<defs>
<clipPath id="highcharts-74tub7h-41">
<rect x="0" y="0" width="255" height="275" fill="none"></rect></clipPath></defs>
<rect fill="none" class="highcharts-background" x="0" y="0" width="441" height="319.5" rx="0" ry="0"></rect>
<rect fill="none" class="highcharts-plot-background" x="156" y="10" width="275" height="255"></rect>
<g class="highcharts-pane-group"></g>
<g class="highcharts-grid highcharts-xaxis-grid ">
<path fill="none" class="highcharts-grid-line" d="M 156 15.5 L 431 15.5" opacity="1"></path>
<path fill="none" class="highcharts-grid-line" d="M 156 36.5 L 431 36.5" opacity="1"></path><path fill="none" class="highcharts-grid-line" d="M 156 56.5 L 431 56.5" opacity="1"></path><path fill="none" class="highcharts-grid-line" d="M 156 76.5 L 431 76.5" opacity="1"></path><path fill="none" class="highcharts-grid-line" d="M 156 97.5 L 431 97.5" opacity="1">
</path><path fill="none" class="highcharts-grid-line" d="M 156 117.5 L 431 117.5" opacity="1"></path><path fill="none" class="highcharts-grid-line" d="M 156 137.5 L 431 137.5" opacity="1"></path><path fill="none" class="highcharts-grid-line" d="M 156 158.5 L 431 158.5" opacity="1"></path><path fill="none" class="highcharts-grid-line" d="M 156 178.5 L 431 178.5" opacity="1"></path><path fill="none" class="highcharts-grid-line" d="M 156 199.5 L 431 199.5" opacity="1"></path><path fill="none" class="highcharts-grid-line" d="M 156 219.5 L 431 219.5" opacity="1"></path><path fill="none" class="highcharts-grid-line" d="M 156 240.5 L 431 240.5" opacity="1"></path><path fill="none" class="highcharts-grid-line" d="M 156 260.5 L 431 260.5" opacity="1"></path></g><g class="highcharts-grid highcharts-yaxis-grid "><path fill="none" stroke="#e6e6e6" stroke-width="1" class="highcharts-grid-line" d="M 155.5 10 L 155.5 265" opacity="1"></path><path fill="none" stroke="#e6e6e6" stroke-width="1" class="highcharts-grid-line" d="M 224.5 10 L 224.5 265" opacity="1"></path><path fill="none" stroke="#e6e6e6" stroke-width="1" class="highcharts-grid-line" d="M 293.5 10 L 293.5 265" opacity="1"></path><path fill="none" stroke="#e6e6e6" stroke-width="1" class="highcharts-grid-line" d="M 361.5 10 L 361.5 265" opacity="1"></path><path fill="none" stroke="#e6e6e6" stroke-width="1" class="highcharts-grid-line" d="M 431.5 10 L 431.5 265" opacity="1"></path></g><rect fill="none" class="highcharts-plot-border" x="156" y="10" width="275" height="255"></rect><g class="highcharts-axis highcharts-xaxis "><path fill="none" class="highcharts-tick" stroke="#ccd6eb" stroke-width="1" d="M 156 15.5 L 146 15.5" opacity="1"></path><path fill="none" class="highcharts-tick" stroke="#ccd6eb" stroke-width="1" d="M 156 36.5 L 146 36.5" opacity="1"></path><path fill="none" class="highcharts-tick" stroke="#ccd6eb" stroke-width="1" d="M 156 56.5 L 146 56.5" opacity="1"></path><path fill="none" class="highcharts-tick" stroke="#ccd6eb" stroke-width="1" d="M 156 76.5 L 146 76.5" opacity="1"></path><path fill="none" class="highcharts-tick" stroke="#ccd6eb" stroke-width="1" d="M 156 97.5 L 146 97.5" opacity="1"></path><path fill="none" class="highcharts-tick" stroke="#ccd6eb" stroke-width="1" d="M 156 117.5 L 146 117.5" opacity="1"></path><path fill="none" class="highcharts-tick" stroke="#ccd6eb" stroke-width="1" d="M 156 137.5 L 146 137.5" opacity="1"></path><path fill="none" class="highcharts-tick" stroke="#ccd6eb" stroke-width="1" d="M 156 158.5 L 146 158.5" opacity="1"></path><path fill="none" class="highcharts-tick" stroke="#ccd6eb" stroke-width="1" d="M 156 178.5 L 146 178.5" opacity="1"></path><path fill="none" class="highcharts-tick" stroke="#ccd6eb" stroke-width="1" d="M 156 199.5 L 146 199.5" opacity="1"></path><path fill="none" class="highcharts-tick" stroke="#ccd6eb" stroke-width="1" d="M 156 219.5 L 146 219.5" opacity="1"></path><path fill="none" class="highcharts-tick" stroke="#ccd6eb" stroke-width="1" d="M 156 240.5 L 146 240.5" opacity="1"></path><path fill="none" class="highcharts-tick" stroke="#ccd6eb" stroke-width="1" d="M 156 260.5 L 146 260.5" opacity="1"></path><path fill="none" class="highcharts-axis-line" stroke="#ccd6eb" stroke-width="1" d="M 155.5 10 L 155.5 265"></path></g><g class="highcharts-axis highcharts-yaxis "><text x="293.5" text-anchor="middle" transform="translate(0,0)" class="highcharts-axis-title" style="color:#666666;fill:#666666;" y="303"><tspan>Gross Spends - $</tspan></text><path fill="none" class="highcharts-axis-line" d="M 156 265 L 431 265"></path></g>
<g class="highcharts-series-group">
<g class="highcharts-series highcharts-series-0 highcharts-bar-series highcharts-color-0 highcharts-tracker " transform="translate(431,265) rotate(90) scale(-1,1) scale(1 1)" clip-path="url(#highcharts-74tub7h-41)">
<!--Target elment--> <rect id="targetElm" x="247" y="59" width="7" height="217" fill="rgb(0,207,151)" class="highcharts-point highcharts-color-0 highcharts-drilldown-point " style="cursor:pointer;"></rect>
etc..................
推荐答案
这个怎么样
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.
这篇关于以编程方式触发 svg 矩形元素中的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!