js替换选中的文字

时间:2023-12-23 09:02:02

替换html中选中的文字

        function replaceSelection() {
if (window.getSelection) {
var selecter = window.getSelection();
var selectStr = selecter.toString();
if (selectStr.trim != "") {
var rang = selecter.getRangeAt(0);
var temp = $("<b>" + selectStr + "<b/>");
rang.surroundContents(temp[0]);
}
} else if (document.selection) {//ie
var selecter = document.selection.createRange();
selecter.select();
var selectStr = selecter.text;
selecter.pasteHTML("<b>" + selectStr + "<b/>");
}
}

  程序员的基础教程:菜鸟程序员