没有事件在加载的 svg 路径上工作,尽管它在之前加载的 svg 上工作
我正在尝试在加载的 svg 元素上的路径上使用 click 事件。但它不适用于加载的 svg。但是,如果 svg 未加载,它可以正常工作。
这段代码完美运行。
<div class="mapdiv">
<?php
echo file_get_contents('a.svg');
?>
</div>
$(document).ready(function () {
$("path").click(function () {
alert("clicked");
});
});
但是当尝试使用 jquery 加载 svg 时,它不起作用。谁能解释为什么会发生这种情况?
$(document).ready(function () {
$("#addImage").click(function () {
$("#fileinput").click();
});
$("#fileinput").change(function () {
if (this.files && this.files[0]) {
let reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
}
});
function imageIsLoaded(e) {
$(".mapdiv").load(e.target.result);
}
$("path").click(function () {
alert("clicked");
});
})
I'm trying to use click event on path on loaded svg element. But it is not working on loaded svg. However, it works perfectly if the svg is not loaded.
This code works perfectly.
<div class="mapdiv">
<?php
echo file_get_contents('a.svg');
?>
</div>
$(document).ready(function () {
$("path").click(function () {
alert("clicked");
});
});
But when trying to load the svg using jquery,it does not work. Can anyone explain why this is happening?
$(document).ready(function () {
$("#addImage").click(function () {
$("#fileinput").click();
});
$("#fileinput").change(function () {
if (this.files && this.files[0]) {
let reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
}
});
function imageIsLoaded(e) {
$(".mapdiv").load(e.target.result);
}
$("path").click(function () {
alert("clicked");
});
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要确保内容已附加到 DOM,您需要等待
.load()
。您可以将其作为回调函数来执行。请阅读此处: .load() | jQuery API 文档。或者使用 .live()。To be sure that the content has been appended to the DOM you need to wait for the
.load()
. You can do that as a call back function. Read mere here: .load() | jQuery API Documentation. OR use the .live().