gpt4 book ai didi

javascript - 加载后将 'onclick' 事件附加到 SVG 元素 'rect'

转载 作者:行者123 更新时间:2023-12-02 14:16:01 25 4
gpt4 key购买 nike

我在 DIV 上加载不同的 SVG,它工作正常,但 onclick 绑定(bind)不起作用。

加载后,我调用 LoadSVGBindings 函数来设置 onclick 绑定(bind)。

其工作的唯一方法是在 FireFox 上进行调试。在 LoadSVGBindings 函数处停止然后恢复一切正常。

//HTML code
<div id="TheSVG"></div>


//LoadSVG function
function LoadSVG(Mode){

$.ajax({
type: "GET",
url: 'data/' + Mode + '.svg',
dataType: "text",
contentType: "charset=UTF-8",
success: function(data) { $('#TheSVG').html(data); },
error: function(request, status, error) {alert("Error: " + error);},
});

SetSVGBindings();
}


//Set after loading the SVG
function SetSVGBindings(){

$('polygon').on('click', function () {
alert("polygon");
});

$('rect').on('click', function () {
alert("rect");
});
}

我不知道出了什么问题或如何使其正常工作。

最佳答案

您需要在成功函数中调用 SetSVGBindings,即在 SVG 加载后。

您在问题中编写的方式 SetSVGBindings 将在 SVG 加载之前调用,并且文档中不会有任何 SVG 元素可供 CSS 选择器绑定(bind)。

关于javascript - 加载后将 'onclick' 事件附加到 SVG 元素 'rect',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39026935/

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