使用jquery在svg中选择图像

时间:2022-11-21 12:20:05

I am working on a small script where I should make some edits on images inside SVG using jquery. This is my svg code :

我正在编写一个小脚本,我应该使用jquery在SVG内部对图像进行一些编辑。这是我的svg代码:

    <svg xmlns:svg="http://www.w3.org/2000/svg"
         xmlns="http://www.w3.org/2000/svg"
         version="1.1"
         width="800"
         height="420"
         id="svg">
     <image class="draggable" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://letscode.ghost.io/content/images/2015/09/*.png" width="800" heigqht="420" id="yassin" >

</image> 
</svg>

I want to select the image by it's id Yassin and then edit the attribute width. PS. I used the getelementbyID and the jquery selector but it doesn't work.

我想通过它的id Yassin选择图像,然后编辑属性宽度。 PS。我使用了getelementbyID和jquery选择器,但它不起作用。

1 个解决方案

#1


2  

jquery:

 var img = $('#yassin');

jQuery Demo

pure javascript:

 var img = document.getElementById('yassin');

The Pure Javascript Demo

纯Javascript演示

To change the Width as you mentioned in comments:

要更改注释中提到的宽度:

$('#yassin').attr('width', 500); //this way you can change any attribute

or just:

$('#yassin').width(500); //this is simply for setting the width value

#1


2  

jquery:

 var img = $('#yassin');

jQuery Demo

pure javascript:

 var img = document.getElementById('yassin');

The Pure Javascript Demo

纯Javascript演示

To change the Width as you mentioned in comments:

要更改注释中提到的宽度:

$('#yassin').attr('width', 500); //this way you can change any attribute

or just:

$('#yassin').width(500); //this is simply for setting the width value