如何选择使用Snap.svg现有的SVG片段如何选择、片段、svg、Snap

2023-09-10 20:21:48 作者:橡皮擦擦不掉有关你的记忆

我想在本地运行 Snap.svg ,但Snap.load()函数,使一个AJAX请求,这是不允许在本地(在Chrome,反正)。下面是我的code:

I'm trying to run Snap.svg locally, but the Snap.load() function makes an AJAX request, which isn't allowed locally (in Chrome, anyways). Below is my code:

window.onload = function () {

    var s = Snap("#headDiv");
    Snap.load("emotions.svg", function(f) {

        eyes = f.select("#eyes");
        lids = f.select("#lids");
        head = f.select("#head");

        s.append(f);
    });
};

所以,虽然这工作得很好,从一台服务器,我想获得这个在本地运行。什么是我最好的选择,包括我的 emotions.svg 文件而不做一个AJAX请求?

So while this works fine from a server, I'd like to get this to run locally. What would be my best option to include my emotions.svg file without making an AJAX request?

我知道它很容易只是扔SVG的DIV,但我不能够访问片段,与我目前的脚本的方式。任何想法?

I know it's easy to just throw the SVG in the DIV, but I wasn't able to access the fragments that way with my current script. Any ideas?

推荐答案

更​​改:

window.onload = function () {

var s = Snap("#headDiv");
Snap.load("emotions.svg", function(f) {

    eyes = f.select("#eyes");
    lids = f.select("#lids");
    head = f.select("#head");

    s.append(f);
});
};

要简单的:

window.onload = function () {

    eyes = Snap.select("#eyes");
    lids = Snap.select("#lids");
    head = Snap.select("#head");
};

,然后把实际的SVG脚本中的目标DIV伟大的工作。

And then placing the actual SVG script in the target DIV worked great.