简体   繁体   English

在svg元素中更改图像

[英]Change image in svg element

I am trying to modify an existing SVG element in an HTML document. 我试图修改HTML文档中的现有SVG元素。 I have the following: 我有以下内容:

<svg class="logo" viewBox="0 0 435 67"> <!-- IMAGE DIMENSIONS -->    
    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#logo--someLogo"></use>
</svg>

I guess it has to be in that xlink:href="#logo--someLogo" but when I try to change it through the browser with a .svg file or .png like 我想它必须在xlink:href="#logo--someLogo"但是当我尝试通过浏览xlink:href="#logo--someLogo" .svg文件或.png来改变它时

$('.logo use').attr('xlink:href', "//someurl.com.png");

it doesnt show anything. 它没有显示任何东西。

So Im wondering how I can change that xlink:href with another source. 所以我想知道如何用另一个来源改变xlink:href

也许这些文档节点有本机方法,但这也可行(如果只有一个use标签)。

document.getElementsByTagName('use')[0].setAttribute('xlink:href', 'link_to_another_image')

After some digging I figured out that the way svg use works is that in the DOM there is a symbol tag that matches that resource specified xlink:href . 经过一番挖掘,我发现svg使用的方式是在DOM中有一个符号标签,它匹配指定的xlink:href资源。 In that tag, there is a path element in which the resource path needs to be specified. 在该标记中,有一个路径元素,需要在其中指定资源路径。

I used a jpeg to svg converter to get the path of the image, and it seems to work when replacing the path in the symbol element in the DOM. 我使用jpeg到svg转换器来获取图像的路径,并且它似乎在替换DOM中的符号元素中的路径时起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM