Rendering HTML inside textarea

后端 未结 7 2075
孤独总比滥情好
孤独总比滥情好 2020-11-22 01:34

I need to be able to render some HTML tags inside a textarea (namely , , , ) but textareas only interpret their content as text. Is

7条回答
  •  我在风中等你
    2020-11-22 02:00

    Since you only said render, yes you can. You could do something along the lines of this:

    function render(){
    	var inp     = document.getElementById("box");
    	var data = `
    
    
    
    ${inp.value} cant touch this
    `; var blob = new Blob( [data], {type:'image/svg+xml'} ); var url=URL.createObjectURL(blob); inp.style.backgroundImage="url("+URL.createObjectURL(blob)+")"; } onload=function(){ render(); ro = new ResizeObserver(render); ro.observe(document.getElementById("box")); }
    #box{
      color:transparent;
      caret-color: black;
      font-style: normal;/*must be same as in the svg for caret to align*/
      font-variant: normal; 
      font-size:13.3px;
      padding:2px;
      font-family:monospace;
    }
    This makes it so that a textarea will render html! Besides the flashing when resizing, inability to directly use classes and having to make sure that the div in the svg has the same format as the textarea for the caret to align correctly, it's works!

提交回复
热议问题