How to add using d3.js selection.text () method

selection.text(' ') appears to convert special characters, so it displays the actual text, not the space as I want. Is there a way to prevent using the text() method to select from this for me? For context, this is a table cell that is empty for some elements in the selection, and I need space there so that the cells display correctly.

+13
javascript
Oct. 14
source share
2 answers
 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; }); }; 

The important line here is that calling .text () to highlight will set the textContent property for each element. Therefore, the right thing in your case is to use

 selection.text(' '); 

So, if you want to use spaces, you just need to use spaces in the text, not encoded html spaces. If you want to use html encoded characters, you need to treat the content as innerHTML.

 selection.html('&nbsp;'); 
+7
Oct 14 '12 at 14:45
source share

Use Unicode JavaScript escape code, not an HTML object. &nbsp; in HTML stands for Unicode character U + 00A0, therefore

 selection.text('\u00A0') 

should do what you want.

+30
Oct 14
source share



All Articles