gpt4 book ai didi

javascript - 以编程方式触发 svg rect 元素中的单击事件

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:58:25 25 4
gpt4 key购买 nike

如何以编程方式触发svg rectangle element元素中的click事件?像 :$("#targetElm").trigger("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'));

这是一个工作示例...

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>

即创建一个点击事件并将其分派(dispatch)给元素。尽管您的问题中似乎没有针对点击事件的任何处理程序。

关于javascript - 以编程方式触发 svg rect 元素中的单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49834459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com