没有事件在加载的 svg 路径上工作,尽管它在之前加载的 svg 上工作

发布于 2025-01-14 04:29:44 字数 865 浏览 4 评论 0原文

我正在尝试在加载的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人如玉 2025-01-21 04:29:44

要确保内容已附加到 DOM,您需要等待 .load()。您可以将其作为回调函数来执行。请阅读此处: .load() | jQuery API 文档。或者使用 .live()

$(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, function() {
    $("path").click(function() {
      alert("clicked");
    });
  });
});

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().

$(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, function() {
    $("path").click(function() {
      alert("clicked");
    });
  });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文