简体   繁体   English

将Raphael SVG存储在JavaScript变量中

[英]Store raphael svg in javascript variable

I've got an interactive SVG that needs to be drawn in multiple different places on my site. 我有一个交互式SVG,需要在网站上的多个不同位置绘制它们。 Currently I can do this by running this on each page. 目前,我可以通过在每个页面上运行它来完成此操作。

$.getScript( UTIL.theme_dir() + '/assets/js/data/lot-plan.js', function() {

    // Plan Interactions
    // Builds the hovers and click events
    obj.planInteractions(lotInfo);

    // When clicking on custom title blocks, highlight approriate units
    $(".custom-title").on('click', function(){
        if( !$(this).hasClass('disabled') ){

            obj.planHighlight(lotInfo, $(this));

        }
    });

    obj.scaleSVG('#lot-plan');

});

But as you can see, this will call the lot-plan.js file each time. 但是如您所见,这每次都会调用lot-plan.js文件。 I've been trying to figure out a way to call it once, and store it's data in a var to reuse. 我一直在试图找到一种方法来调用它一次,并将其数据存储在var中以便重用。 This lot-plan file is a Raphael SVG, so once the code is loaded, it renders the svg on the page. 该批次计划文件是Raphael SVG,因此一旦加载代码,它将在页面上呈现svg。 Loading this file also gives me access to the "lotInfo" object, which has some config stuff in it. 加载该文件还使我可以访问“ lotInfo”对象,该对象中包含一些配置内容。

I've been trying to call the file with getScript and store it in a variable, and check if that variable is set, but I'm unable to access the js from the file, basically, the svg doesn't render. 我一直在尝试使用getScript调用文件并将其存储在变量中,并检查是否设置了该变量,但是我无法从文件访问js,基本上,svg无法呈现。

Am I going about this all wrong? 我要解决所有这些错误吗? Can anybody provide any insights into this? 有人可以对此提供任何见解吗? Please ask if you need any more info / code, i'll be happy to provide. 请询问您是否需要更多信息/代码,我很乐意提供。

Juat incase you want to consider XMLHTTPRequest per my comment above.. 如果您想根据以上我的评论考虑XMLHTTPRequest ,那么Juat。

You can receive an xml string that can be used to fill innerHTML for a Div on your web page. 您可以在网页上收到一个xml字符串,该字符串可用于填充Div的innerHTML。 And/Or you can work with the elements as XML nodes then clone them into your local SVG. 和/或您可以将元素用作XML节点,然后将其克隆到本地SVG中。

var  XMLdoc
function loadSVGasXML()
{
    var SVGFile="yourSVGFile.svg"
    var loadXML = new XMLHttpRequest;
    function handler()
    {
        if(loadXML.readyState == 4)
        {
            if (loadXML.status == 200) //---loaded ok---
            {
                //---responseText---
                var xmlString=loadXML.responseText
                //---mySVGDIV.innerHTML=xmlString
                //---DOMParser---
                var parser = new DOMParser();
                XMLdoc=parser.parseFromString(xmlString,"text/xml").documentElement ;
            }
        }
    }
    if (loadXML != null)
    {
        loadXML.open("GET", SVGFile, true);
        loadXML.onreadystatechange = handler;
        loadXML.send();
    }
}

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

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