javascript - 使用 D3JS 的 div 中的 img 和 p

标签 javascript d3.js

我想要一个包含图像和图像标题的 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/

相关文章:

javascript查询字符串

javascript - JQuery .post() 将 html 返回到表中

javascript - 如何在用户启用他们的 javascript 后立即重定向到页面?

javascript - 如何使 getElementsByClassName 对所有元素进行更改?

arrays - D3.js 未知的列数和行数

javascript - ExtJS 4 到 ExtJS 5 的迁移

javascript - 如何在 D3 圆包中将文本宽度与圆圈大小相匹配

jquery - d3.js Treemap - 有没有办法指定显示首选项(排序)?

d3.js - 旋转 D3 符号

javascript - 为什么我的 div 值未定义?