document.selection 的 empty() 与 clear() 特殊用法

作者:vkvi 来源:ITPOW(原创) 日期:2008-11-12
  • document.selection.empty() 让选中的内容不选中;
  • document.selection.clear() 删除选中的内容。

很好理解,也很好区分,这里讲的是一个特殊用法。

document.getElementById("content").focus();
var r = document.selection.createRange();
document.selection.empty();
r.text = str;

和下面的代码:

document.getElementById("content").focus();
var r = document.selection.createRange();
document.selection.clear();
r.text = str;

都是在光标处插入文字:如果有选中的内容,则会替换选中的内容;如果没有选中的内容,则 empty() 那段代码会在光标处插入文字,而 clear() 那段代码会先删除光标后面一个字符再插入文字。这点区别还是很重要的。

相关阅读

相关文章