获取加载的SVG文件的内容 [英] Get content of loaded SVG file

查看:390
本文介绍了获取加载的SVG文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了将从url加载到特定div svg文件的函数:

I have made function that will load to specific div svg file from url:

function load_vector(divId, vector_url) {
    var div = $(divId);

    div.load(vector_url);

    console.log(div.find('svg').html());
}

我这样调用这个函数:

load_vector("#myDiv", "/path/to/vector/file.svg");

它成功地将svg文件加载到这个div,现在我在页面中将它作为html。

It successfully loads svg file to this div and now I have it as html in my page.

但是我无法获得此svg文件的内容

However I can't get contents of this svg file

console.log(div.find('svg').html());

这将返回 undefined

我错过了什么?

推荐答案

需要等到加载过程完成(它是异步的)。在回调函数中包装 console.log 。类似于:

Need to wait until the load process is complete (it is async). Wrap your console.log inside a callback function. Something like:

div.load(vector_url, function() {
    console.log(div.find('svg').html());
});

这篇关于获取加载的SVG文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆