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

后端 未结 2 774
失恋的感觉
失恋的感觉 2021-01-04 02:06

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 prev

相关标签:
2条回答
  • 2021-01-04 02:49
    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() on a selection will set the textContent property on each element. So the correct thing to do in your case is use

    selection.text(' ');
    

    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;');
    
    0 讨论(0)
  • 2021-01-04 02:53

    Use a JavaScript Unicode escape rather than an HTML entity. &nbsp; in HTML denotes the Unicode character U+00A0 so

    selection.text('\u00A0')
    

    should do what you want.

    0 讨论(0)
提交回复
热议问题