开发者

execCommand on Range

开发者 https://www.devze.com 2023-03-17 01:24 出处:网络
I\'ve been calling execCommand on the document to make the selected text bold or to set its color. But开发者_如何学编程 recently I need to use execCommand on a certain range and not the selected text.

I've been calling execCommand on the document to make the selected text bold or to set its color. But开发者_如何学编程 recently I need to use execCommand on a certain range and not the selected text.

Can I do this and if so how?


You can, but it needs to be the selection. So in other words, do the following:

  • Store the current selection
  • Make a new selection based on the Range
  • Perform the execCommand
  • Restore the previous selection

You can create a selection from ranges (non-IE browsers) with the following:

 var selection = window.getSelection();
 selection.removeAllRanges();
 selection.addRange(range);

With IE, you can directly execute execCommand on TextRange objects, so this whole process won't be necessary.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号