SVG-based text input field

前端 未结 4 2148
孤街浪徒
孤街浪徒 2020-12-13 18:44

Has anyone seen any javascript implementation of a text input field besides http://www.carto.net/papers/svg/gui/textbox/ ?

4条回答
  •  天命终不由人
    2020-12-13 19:37

    This is for MS Internet Explorer, not tested in any other browser.

    As mentioned in another comment, up to IE11 does not support foreignObject. Instead, use the contentEditable attribute.

    Plain SVG example

    
        
            foo
        
    
    

    D3.js example with data binding

    Here we listen for key events to write the text back to the data.

    selection.
      .append("text")
        .attr("contentEditable", true)
        .text(function(d) { return d.text })
        .on("keyup", function(d) { d.text = d3.select(this).text(); });
    

    Note: If nodes are dynamically generated like with d3.js, you must capitalize contentEditablelike such (this took me some time)!

    Note: Do not style your text with pointer-events: None, for then you cannot interact with the text anymore, regardless of the contentEditable setting.

提交回复
热议问题