问题描述
selection.text('& nbsp;')
出现转换特殊字符,因此它呈现实际文本,而不是像我想要的空格。是否有一种方法来阻止 text()
方法对我做这个转义的选择?对于上下文,这是一个表单元格,对于选择中的一些元素是空的,我需要在那里的空间,以便单元格正确渲染。
selection.text(' ')
appears to convert the special characters so it renders the actual text, rather than a space like I want. Is there a way to prevent the text()
method on selections from doing this escaping for me? For context, this is a table cell that is empty for some elements in the selection, and I need the space in there so that the cells render properly.
推荐答案
d3_selectionPrototype.text = function(value) {
return arguments.length < 1 ? this.node().textContent : this.each(typeof value === "function" ? function() {
var v = value.apply(this, arguments);
this.textContent = v == null ? "" : v;
} : value == null ? function() {
this.textContent = "";
} : function() {
this.textContent = value;
});
};
这里重要的一点是在选择上调用.text()元件。所以在你的情况下,正确的事情是使用
The important line here is that calling .text() on a selection will set the textContent property on each element. So the correct thing to do in your case is use
selection.text(' ');
所以如果你想使用空格,那么你只需要在文本中使用空格,空间。如果要使用html编码的字符,则必须将内容视为innerHTML。
So if you want to use spaces then you simply have to use spaces in the text, not html encoded spaces. If you want to use html encoded chars then you have to treat the content as innerHTML.
selection.html(' ');
这篇关于如何添加& nbsp;使用d3.js selection.text()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!