How do I wrap a text selection from window.getSelection().getRangeAt(0) with an html tag?

前端 未结 3 1070
忘掉有多难
忘掉有多难 2020-11-30 14:14

How do I take a selection from window.getSelection().getRangeAt(0) and surround it with an HTML tag such as \'span\' or \'mark\'? I prefer a straight javascript or jQuery so

3条回答
  •  醉话见心
    2020-11-30 14:48

    (Answering my own question based on a similar question I found when I posted mine...)

    The guys in this Q&A thread were on an interesting track. It just used a different format than I was looking for. Modifying their code, I was able to do the following:

    Display Range | Mark Range

    This is sample text. You should be able to type in this box or select anywhere in this div and then click the link at the top to get the selected range.
提交回复
热议问题