Javascript 当图像在D3js中不存在时,是否更改svg图像的xlink:href链接?

Javascript 当图像在D3js中不存在时,是否更改svg图像的xlink:href链接?,javascript,d3.js,Javascript,D3.js,我将svg图像添加到图表中。但有时图像源可能不存在。为了避免这种情况,我用默认图像替换了不存在的图像 g.selectAll("image") .data(data) .enter() .append("svg:image") .attr("xlink:href",function(d){ return d.name+".jpg"; }) .attr("x", function (d,i) { return x(d.x); } ) .attr(

我将svg图像添加到图表中。但有时图像源可能不存在。为了避免这种情况,我用默认图像替换了不存在的图像

 g.selectAll("image")
 .data(data)
 .enter()
   .append("svg:image")
   .attr("xlink:href",function(d){
       return d.name+".jpg";
    })
   .attr("x", function (d,i) { return x(d.x); } )
   .attr("y", function (d,i) { return y(100); } )
   .attr("width",imageSize)
   .attr("height",imageSize);
假设目录中不存在d.name+“.jpg”。我必须在default.jpg中替换它

我该怎么做


提前感谢。

一种方法是使用
图像处理程序:


var svg=d3.select('body')
.append('svg')
.attr('width',500)
.attr('height',500);
var img=svg.append(“图像”)

.attr(“xlink:href”,“doesnotextit.jpeg”)//这可能是一个副本,它的答案对您有用。这将对您有所帮助