contenteditable on doubleclick

前端 未结 3 1576
我寻月下人不归
我寻月下人不归 2021-01-11 19:44

I\'ve got a p tag with some text, and I\'m trying to make it contenteditable but only on doubleclick. How can I prevent browser from placing the cu

3条回答
  •  梦谈多话
    2021-01-11 20:20

    Here is a solution which works for me:

    
    
    

    Double-click the text to begin editing.

    Here is a working fiddle: https://jsfiddle.net/dtL3xzfb/

    It uses a timeout event to check if the text has been selected within a certain period of time (300ms), if the text is not selected (by a second click) it will set content editable to false again: The content editable parameter is set to true after the first click so text will be selected when you click for a second time.

提交回复
热议问题