我有一个函数可以获取选择的文本,这个文本是通过鼠标选择的,并将其添加到一个变量中。我想在所选文本中添加 `` 标签 - 在该段落中。
我可以获取文本选择变量,但是我的函数没有将
我该如何将其替换在段落中呢? 谢谢。
$("p").live("mouseup",function() {
selection = getSelectedText();
if(selection.length >= 3) {
var $spn = $("<span></span>").html(selection).addClass("selected");
$("p").wrap($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;
}
}
我可以获取文本选择变量,但是我的函数没有将
<span></span>
放在段落<p>
中选定的文本周围,而是将其包装在外部。我该如何将其替换在段落中呢? 谢谢。