最近在做编辑器,需要对编辑器研究下,发现document.selection.createRange这个挺有用的。
|
document.selection.createRange() 根据当前文字选择返回 TextRange 对象,或根据控件选择返回 ControlRange 对象。配合 execCommand,在 HTML 编辑器中很有用,比如:文字加粗、斜体、复制、粘贴、创建超链接等。 一个简单的例子
<html><head><title>document.selection.createRange例子</title></head><body> <div>请选中这里的部分文字。</div><div><input type="button" value="加粗" onclick="javascript :Bold();" /></div> <script language="javascript"> function Bold(){ var bo = document.selection.createRange(); bo.execCommand("Bold"); }</script> </body></html> |
本文介绍了一个实用的方法,利用document.selection.createRange()实现HTML编辑器的文字加粗等功能。此方法能够根据当前选择的内容返回TextRange对象,并通过execCommand执行如加粗、斜体等操作。
1万+

被折叠的 条评论
为什么被折叠?



