【发布时间】:2018-03-08 10:59:50
【问题描述】:
我正在为我的 D3 项目中的鼠标悬停事件制作带有图像和其他数据的工具提示。当我的数据图像丢失时,我想使用默认图像。我无法使用我编写的 html 字符串出现错误,如果有人可以帮我看一下,我将不胜感激。我认为我在格式化这个字符串时遇到了麻烦。
这是相关的代码:
.on("mouseover", function(d) {
d3.select(this)
.transition()
.duration(50)
div.transition()
.duration(200)
.style("opacity", .9);
div.html('<img src="images/'+ d.properties.objectNumber +'.jpg" onError="this.onerror=null;this.src="images/00037631.jpg" style ="width:4em;" /> ' + d.properties.name)
//div.html('<img src="images/'+ d.properties.objectNumber +'.jpg" onError="this.src="images/ANMS0533[020].jpg" style ="width:4em;" /> ' + d.properties.name)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
我的图像在它存在的地方显示良好,但如果图像丢失,我会得到一个无图像图标,我希望看到默认图像,或者什么都没有。
希望有人能帮忙。
【问题讨论】:
-
这可能是由
onError期待一个函数引起的。 -
感谢您的回复。所以我不能像这样在 html 中使用 on error 吗?我不确定如何在这里使用函数。您能否提供示例代码的帮助或将我指向可以提供帮助的资源?再次感谢您不厌其烦地回复。
标签: javascript html image d3.js onerror