简体   繁体   English

外部,动态加载 SVG:无法使用 JavaScript 访问元素

[英]External, dynamically loaded SVG: Can't access Elements with JavaScript

I am programming a digital advent calendar for a client.我正在为客户编写数字降临节日历。 This thing where you can open a little door every day until christmas.这东西你可以每天打开一扇小门直到圣诞节。 Here is my problem: I have 24 svgs where every day one more door is open and you can click on "todays" door to open it.这是我的问题:我有 24 个 svg,每天都会打开一扇门,您可以单击“今天”门打开它。 So i have overlays in the svgs which trigger a modal where the content is dynamically loaded.所以我在 svgs 中有叠加层,它触发了一个动态加载内容的模态。

Also the SVG itselfs needs to be dynamically loaded since it is a new one every day. SVG 本身也需要动态加载,因为它每天都是新的。 When i run it locally everything works fine but i need to implement it into the customers Webflow.com environment.当我在本地运行它时,一切正常,但我需要将其实施到客户 Webflow.com 环境中。 The svgs are stored on my own server. svgs 存储在我自己的服务器上。 As soon as i load it in the webflow environment i cannot access the elements inside the svg anymore.一旦我在 webflow 环境中加载它,我就无法再访问 svg 中的元素。 I guess this is because it is not loaded into the DOM properly.我想这是因为它没有正确加载到 DOM 中。 I can't find any solution.我找不到任何解决方案。

So this is the code block that i embed into the webflow page:所以这是我嵌入到 webflow 页面的代码块:

<div class="backgroundcontainer">
    <object class="backgroundimage" data="" id="background" type="image/svg+xml"></object>
</div>

then via javascript i add the image:然后通过 javascript 我添加图像:

let bgImage = document.getElementById('background');
if (bgDay >= 25) {
    bgImage.data = "MYSERVER/assets/images/bg/25.svg"
} else {
    bgImage.data = "MYSERVER/assets/images/bg/" + bg;
}

bg is "today".svg bg是“今天”.svg

locally this works to add the click listener to the 24 "doors" (with ids 1-24 within the svg) which are defined in the svg:在本地,这可以将点击侦听器添加到 svg 中定义的 24 个“门”(svg 中的 ID 为 1-24):

bgImage.addEventListener('load', function () {
    let bgContent = bgImage.contentDocument;

    for (let i = 1; i <= today; i++) {
        let door = bgContent.getElementById(i);
        door.style.cursor = 'zoom-in';
        door.setAttribute('id', i.toString())
        door.addEventListener('click', function () {
            openModal(i)
        })
    }
})

in webflow bgImage.contentDocument returns null在 webflow bgImage.contentDocument返回null

any ideas?有任何想法吗? Thanks谢谢

It doesn't matter where your SVGs are hosted. SVG 的托管位置并不重要。 Referencing them dynamically is also not an issue, however I've not seen <object> used for this purpose before.动态引用它们也不是问题,但是我以前没有看到<object>用于此目的。

In your case, I'd build out the Webflow site with all of the components you want, and use a standard Image element, with an id eg background .在您的情况下,我将使用您想要的所有组件构建 Webflow 站点,并使用带有id例如background的标准 Image 元素。

Then your button click script updates the img src URL and opens the modal.然后您的按钮单击脚本更新 img src URL 并打开模式。

Note your use of the word "background" is a bit mystifying, since it suggests you're trying to set a background image- which is a style attribute.请注意,您对“背景”一词的使用有点令人费解,因为它表明您正在尝试设置背景图像——这是一个样式属性。 The approach here would be similar, but with some variations to your script.这里的方法是相似的,但对您的脚本有一些变化。

Also...还...

i cannot access the elements inside the svg anymore我无法再访问 svg 中的元素

If you mean that you're seeing the SVG successfully, but unable to access the inner structure of the SVG for eg class based CSS coloring, then the problem is that the SVG must be inline.如果您的意思是您成功看到 SVG,但无法访问 SVG 的内部结构,例如基于 CSS 着色的 class,那么问题是 SVG 必须是内联的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM