用js给svg添加image元素

103 阅读1分钟

html代码: <svg id="svg" xmlns="http://www.w3.org/2000/svg"></svg>

javascript代码:

`const svgElement = document.getElementById('svg');

const img = document.createElementNS('www.w3.org/2000/svg', 'image');

img.href.baseVal = "./test.png" ;

img.setAttribute('height', '220px');

img.setAttribute('width', '220px');

svgElement.appendChild(img);`