How to make selected text bold/italic/underlined in javascript?

后端 未结 3 1760
生来不讨喜
生来不讨喜 2020-12-31 18:40

I\'m trying to work on a webpage that allows users to write their own notes for a school project, and my idea was to let them bold/italicize/underline their text using butto

3条回答
  •  天涯浪人
    2020-12-31 19:37

    You can use execCommand(). This API was meant for developing text editors. The 3 buttons utilize the very versatile execCommand() and the writing element is a plain div enabled with the attribute contenteditable.

    SNIPPET

    
    
    
    
      
      
    
    
    
    
      
    The quick brown fox jumped over the lazy dog.

提交回复
热议问题