javascript - 如何使用js从json中提取图片并显示在html标签中
问题描述
这是我的json:
{ "type": "Feature", "properties": { **"pic": "/logo2.png",** "nombre": "T2V", "x": -4.45497, "y": 36.692029, "field_8": null, "field_9": null }, "geometry": { "type": "Point", "coordinates": [ -4.45497, 36.692029 ] } },
这是尝试在相对路径中提取图片但不起作用的代码部分:
var info = L.control();
info.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'info');
this.update();
return this._div;
};
info.update = function (props) {
// console.log(props);
this._div.innerHTML = ''
+(props? '<b><center style="color:#838383; margin-top:10px;">'+props.nombre+'</center></b>': '')
+(props? '<br><b><center style="color:#838383">'+props.web+'</center></b>': '');
+(props? '<br><img src="'+props.pic+'"style="width:40px;height:40px;">': '');
this._div.innerHTML += '<br /><img src="link.png" style="background:rgba(254,198,41,0.8); margin-left:20px; margin-right:15px; margin-top:0px">';
};
info.addTo(map);
谁能帮我举一些关于这个动作的例子?
解决方案
好的,您可以为 img 元素创建一个变量,然后将属性“src”设置为指向您的 json 对象的相对或绝对路径。
var image = document.createElement('img');
image.setAttribute('src', json.properties.pic);
然后将该元素插入到您希望的容器元素中:
document.getElementById('img-container').appendChild(img);
推荐阅读
- javascript - d3.json observablehq 的项目限制
- c# - 我应该什么时候处理异常,什么时候应该抛出它们?
- ruby-on-rails - 在 ar_multidb 中拆分读取到从属并写入到主控
- javascript - 动态数据绑定不起作用 Angular Chrome 扩展
- python - 同一个类中的同名函数 - 有没有一种优雅的方法来确定调用哪个?
- mongodb - 撇号 cms 版本 2.65.0 在运行项目时出现错误
- c++ - 以下递归代码的时间复杂度是多少
- html - FontAwesome 图标不可见,但适用于另一个相同的视图
- c# - 这个字符串的类型是什么以及如何处理它?
- css - scss - 开始一个部分命名的类