【发布时间】:2011-06-02 00:10:34
【问题描述】:
当出现多次时,我很难选择文本并替换该选择。它总是恢复到第一次出现并替换它。
这是我正在使用的脚本,如果选择仅在我完美时出现。当它出现不止一次时,它会抓住第一个。
var self=$('#textarea');
GetSelected={};
GetSelected=function(){
var txt='';
if(window.getSelection){
txt=window.getSelection();
}
else if(document.getSelection){
txt=document.getSelection();
}
else if(document.selection){
txt=document.selection.createRange().text;
}
return txt;
}
self.html(self.html().replace(selection, '<b>' + selection + '</b>'));
有什么我缺少的东西,知道要替换哪个选择吗?
【问题讨论】:
标签: javascript jquery getselection