这个问题涉及一个类似的问题。 问题得到了回答,但这一回答有一个重大缺陷,即如果文件中所选择的同一案文在其他地方存在,则<代码><span>对复制件进行包装,而不是有关案文。
I realize this maybe be against some stackoverflow protocol, but I m posting here without a real clue as to how to proceed. My best guess would be to somehow find the string length before the selected text (something along these lines) but how to incorporate that in to the replacement function itself ... well I could use a push. Anyone?
(我已接过前一个职位的解决办法(如下:athias-bynens)。)
$("p").live("mouseup",function() {
selection = getSelectedText();
if(selection.length >= 3) {
var spn = <span class="selected"> + selection + </span> ;
$(this).text($(this).html().replace(selection, spn));
}
});
//Grab selected text
function getSelectedText(){
if(window.getSelection){
return window.getSelection().toString();
}
else if(document.getSelection){
return document.getSelection();
}
else if(document.selection){
return document.selection.createRange().text;
}
}