javascript - execCommand on Range - Stack Overflow

I've been calling execCommand on the document to make the selected text bold or to set its color.

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?

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?

Share Improve this question edited Jul 2, 2011 at 10:22 alex 491k204 gold badges889 silver badges991 bronze badges asked Jul 2, 2011 at 10:20 JoshuaJoshua 15.5k24 gold badges101 silver badges174 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742360021a4429205.html

相关推荐

  • javascript - execCommand on Range - Stack Overflow

    I've been calling execCommand on the document to make the selected text bold or to set its color.

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信