我想要一个包含图像和图像标题的 div。我设法使用以下代码获取图像,但无法在 div 中组合图像和文本。
function update(sel) {
console.log('update')
console.log(sel)
update_images(d3.select("#fotoos"), sel);
}
function update_images(ul, data) {
var image = ul.selectAll("img").data(data);
image.exit().remove();
image.enter().append("img");
image.attr("class", "foto");
image.attr("src", function(d) {
var u = '{{ =URL('download') }}'
return u.concat("/", d.file) })
}
如何获得以下内容?
<div>
<p ...
<img ...
</div>
谢谢, 理查德
注意:{{ }} 代码确实来自 Web2Py
最佳答案
您可以在 http://jsfiddle.net/zceg2/1/ 查看工作示例.
您需要先创建容器元素(在本例中为 div
),然后使用该选择附加您的文本和图像。
var data = [{id: 1, text: 'sample text 1', imgsrc: 'http://placehold.it/100x100'},
{id: 2, text: 'sample text 2', imgsrc: 'http://placehold.it/100x100'},
{id: 3, text: 'sample text 3', imgsrc: 'http://placehold.it/100x100'},
{id: 4, text: 'sample text 4', imgsrc: 'http://placehold.it/100x100'}];
var gallery = d3.select('body').append('div');
var container = gallery.selectAll('.container')
.data(data, function(d) { return d.id; });
container.enter().append('div')
.attr('class', 'container')
container.exit().remove();
container.selectAll('.text')
.data(function(d) { return [d]; })
.enter().append('p')
.attr('class', 'text')
.text(function(d) { return d.text; });
container.selectAll('.picture')
.data(function(d) { return [d]; })
.enter().append('img')
.attr('class', 'picture')
.attr('src', function(d) { return d.imgsrc; });
关于javascript - 使用 D3JS 的 div 中的 img 和 p,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13012262/