从xml响应中获取svg元素

时间:2022-11-27 17:28:55

When i try to access the reponse xml i get errors. i want to access the svg firstly and then get the height and width of the svg viewbox or an alternative way. how can i access the svg from the response xml to get the height and width?

当我尝试访问响应xml时,我得到错误。我想首先访问svg,然后获取svg视图框的高度和宽度或另一种方式。如何从响应xml访问svg以获取高度和宽度?

I want to access the height and width using javascript (maybe jquery) like so:

我想使用javascript(也许是jquery)访问高度和宽度,如下所示:

this._imageW = jQuery(".SVGImage").width(); this._imageH = jQuery(".SVGImage").height();

this._imageW = jQuery(“。SVGImage”)。width(); this._imageH = jQuery(“。SVGImage”)。height();

i am using a method to download the file and then give me the response in xml. idk how to access the elements withing the xml reponse thought.

我正在使用一种方法下载文件,然后在xml中给我回复。 idk如何使用xml响应思想访问元素。

从xml响应中获取svg元素

this is the reponse above. i tried this in the console but doesn't work 从xml响应中获取svg元素

这是上面的反应。我在控制台中尝试了这个但是不起作用

how can i access the height and width of the svg element?

如何访问svg元素的高度和宽度?

1 个解决方案

#1


2  

You're going to want to parse the string c into HTML using the jQuery function $.parseHTML().

您将要使用jQuery函数$ .parseHTML()将字符串c解析为HTML。

From that, you will then have a variable which can be accessed by your getElementByX() calls and can find the SVG element you want.

然后,您将拥有一个可以通过getElementByX()调用访问的变量,并且可以找到所需的SVG元素。

var c = '<svg width="200px" height="100px" viewbox="0 0 100 50"> <path d="M50,35 a20,20 0 1,0 0,-20 a20,20 0 1,0 0,20z" fill="white" stroke="black"></path> </svg>'

var html = $.parseHTML(c);

$('#width').html($(html).attr('width'));

$('#height').html($(html).attr('height'));

$('#output').html($(html));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Height: <span id="height"></span>
</p>
<p>Width: <span id="width"></span>
</p>

<div id="output"></div>

#1


2  

You're going to want to parse the string c into HTML using the jQuery function $.parseHTML().

您将要使用jQuery函数$ .parseHTML()将字符串c解析为HTML。

From that, you will then have a variable which can be accessed by your getElementByX() calls and can find the SVG element you want.

然后,您将拥有一个可以通过getElementByX()调用访问的变量,并且可以找到所需的SVG元素。

var c = '<svg width="200px" height="100px" viewbox="0 0 100 50"> <path d="M50,35 a20,20 0 1,0 0,-20 a20,20 0 1,0 0,20z" fill="white" stroke="black"></path> </svg>'

var html = $.parseHTML(c);

$('#width').html($(html).attr('width'));

$('#height').html($(html).attr('height'));

$('#output').html($(html));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Height: <span id="height"></span>
</p>
<p>Width: <span id="width"></span>
</p>

<div id="output"></div>