HTML / [removed] Disable HTML CONTENT in contentEditable=true

前端 未结 4 1348
你的背包
你的背包 2021-01-03 11:32

What I want?


I want a div what works like a textarea, I don\'t want to have the ability to edit things in the div, and paste imag

4条回答
  •  不知归路
    2021-01-03 11:56

    Resizable Textarea using pure JavaScript without frameworks:

    
        
            
            ItzWarty - Untitled Document
        
        
            
        
    
    

    Very hackish, put it together in less than 10 minutes. Hopefully it'll at least give you an idea.

    ONLY tested on Google Chrome 5.0.308.0

    Explanation of code, since i fail at commenting
    1) before window.onload, the textarea of id "resizableTA" has been created and appended to document.body of DOM tree.
    2) window.onload attaches an event handler, taOnInput [textarea on input].
    3) textarea on input creates a dummy span, forces its width to the width of the textarea and font style to "Lucida Console", which AFAIK is the default font for textareas, copies the value of the textarea to the span's innerHTML, while replacing %0A [newline that textareas use] with
    [line break]...
    4) span's offsetHeight is the height of the span, which can now be used to force the height of the textarea.

    Can anyone confirm that Lucida Console is the default font of textarea? Is it Consola? Courier New? I assumed any fixed-width font would work. I don't use Mac, so I dont know what fonts it shared with windows, though i think Courier New is a better choice...

提交回复
热议问题