Get the word upon which the caret sits within a contenteditable div?

前端 未结 2 1438
醉酒成梦
醉酒成梦 2020-12-17 02:07

I am trying to extract a single word from a content editable div at the position, when the mouse is clicked. For example:

Lorem ipsum dolor sit amet, con

2条回答
  •  天命终不由人
    2020-12-17 02:56

    BitBucket have released their Cursores.js library that does this, it's small and focused which is nice - http://cursores.bitbucket.org/

    The only issue I have is that it doesn't pick up the token if there is no text to the left of the caret, for example "t|est" would work but "|test" wouldn't.

提交回复
热议问题