I'm looking for function which allows me to build some element before or after selected text. Something similar like this one javascript replace selection all browsers but for adding some content before or after selection instead of replacing it, like after()
and before()
jQuery methods. Should I use some DOM selection method, if yes which one? Or does exist something easier to carry it out?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
- jQuery add and remove delay
Here's a pair of functions to do this.
Live example: http://jsfiddle.net/hjfVw/
Code:
In MSIE: collapse the given range and the use pasteHTML to insert the element
Others: Also collapse the given Range and insert the element via insertNode
Both collapse-methods accept an optional argument which defines to where you want to collapse to. If you want to put the element at the end, collapse to the end, otherwise to the start.