简体   繁体   English

将HTML代码添加到D3文本

[英]Adding html code to D3 text

I'm trying to add a link to my string using D3. 我正在尝试使用D3将链接添加到我的字符串。 I am building a list, but I also want to offer the possibility to remove items from that list. 我正在建立一个列表,但我也想提供从该列表中删除项目的可能性。 This is the code: 这是代码:

            for (i in comparison) {
            var t = d3.select("#comparison")
                    .select("ul")
                    .append("li")
                    .text('<a onClick="removeItemComparison(' + i + ')">[X]</a> | ' + comparison[i].properties.BCNAAM);
                  }

However, instead of showing a clickable link, the code is added as a string. 但是,不是显示可单击的链接,而是将代码添加为字符串。

I've tried searching, but I'm not sure what search terms to use. 我尝试搜索,但不确定使用哪些搜索词。 Thanks in advance! 提前致谢!

To add html code to a selection, use selection.html(). 要将html代码添加到选择中,请使用selection.html()。 See also https://github.com/mbostock/d3/wiki/Selections#html 另请参阅https://github.com/mbostock/d3/wiki/Selections#html

What you are probably looking for is the selection.on() method. 您可能正在寻找的是selection.on()方法。 You can use this method to assign event listeners to your items, which can then handle the removal for you. 您可以使用此方法为项目分配事件侦听器,然后可以为您处理删除。

So, instead of... 所以,而不是...

.text('<a onClick="removeItemComparison(' + i + ')">[X]</a> | ' + comparison[i].properties.BCNAAM);

You would do something closer to... 你会做些接近...

.on('click', function(i) { ...doStuffHere })

The API Reference page that contains the details of the on() method can be found here, https://github.com/mbostock/d3/wiki/Selections#on 包含on()方法详细信息的API参考页面可在以下位置找到, 网址为https://github.com/mbostock/d3/wiki/Selections#on

I have not found myself needing to insert html text directly into my D3 code before, so I cannot provide much assistance on that front. 我之前没有发现自己需要直接将html文本插入D3代码中,因此在这方面我无法提供太多帮助。

I hope that helps! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM