解决js设置svg image,图片无法显示问题

用setAttribute来设置svg的image的路径,图片无法显示

要用  image.href.baseval  才会有效

var svgDoc = document.getElementById("sketchpad");
var imgSVG = document.createElementNS("http://www.w3.org/2000/svg", 'image');
imgSVG.href.baseVal = "img/water.png"; //svg设置image的url图片,要用image.href.baseVal
imgSVG.setAttribute('width', 70);
imgSVG.setAttribute('height', 70);
imgSVG.setAttribute('class', 'equ');
imgSVG.setAttribute('x', "150");
imgSVG.setAttribute('y', "200");
svgDoc.appendChild(imgSVG);

 

你可能感兴趣的:(web前端,svg)