为什么.html()不适用于使用jquery的SVG选择器? [英] Why .html() doesn't work with SVG selectors using jquery ?

查看:316
本文介绍了为什么.html()不适用于使用jquery的SVG选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题可以告诉我如何将我的SVG元素转换为字符串?



我正在使用

.html()利用了 innerHTML code>这是为html,而不是svg。 SVG是xml文档,因此您可以使用 XMLSerializer()

  var svg = document.getElementById('svg_root'); //或任何你称之为的
var serializer = new XMLSerializer();
var str = serializer.serializeToString(svg);


Question can some one tell me how can i convert my SVG element to a string ?

i'm using canvg to convert my SVG to an image.

it has to be render in a canvas first , the canvg() method is expecting a SVG STRING

code :

  function updateChartImage(){
        canvg(document.getElementById('canvas'),expecting ` svg string`);
       var canvas = document.getElementById("canvas") ; 
       var img = canvas.toDataURL("image/png");
       img = img.replace('data:image/png;base64,', '');
       $("#hfChartImg").val(img) ;
       $('#img').attr({ src: img });
        }

i have tried

$('#container svg').html() ; // it gives me an error 
//Uncaught TypeError: Cannot call method 'replace' of undefined 

notice that

  $('#container svg') 
  $('#container').html() // both works fine and

UpDATE

i'm using highcharts the have a getSVG() function that i can pass to the canvg() but the problem is it dosen't get the latest updates , so i have to do it this way , when running the getSVG() function i get the following :

LINK

解决方案

As far as I can recall jQuery's .html() makes use of innerHTML which is meant for html, not svg. SVG's are xml documents, so you can use XMLSerializer()

var svg = document.getElementById('svg_root'); // or whatever you call it
var serializer = new XMLSerializer();
var str = serializer.serializeToString(svg);

这篇关于为什么.html()不适用于使用jquery的SVG选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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