以下为加强版本,核心要点,直接替换myValue 改成选择的内容
var selectValue = myField.value.substring(startPos, endPos); //选取内容
myValue = "[b]"+ selectValue + "[/b]";
function insertAtCursor(myField, myValue) {
bottom.innerHTML = "0";
if (document.selection) {
//IE support
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
sel.select();
// bottom.innerHTML = "1";
} else if (myField.selectionStart || myField.selectionStart == '0') {
//MOZILLA/NETSCAPE support
var startPos = myField.selectionStart;// 选中起点
var endPos = myField.selectionEnd; //选中终点
var beforeValue = myField.value.substring(0, startPos); //起点之前内容
var afterValue = myField.value.substring(endPos, myField.value.length); //终点最后内容
var selectValue = myField.value.substring(startPos, endPos); //选取内容
myValue = "[b]"+ selectValue + "[/b]";
myField.value = beforeValue + myValue + afterValue;
myField.selectionStart = startPos + myValue.length;
myField.selectionEnd = startPos + myValue.length;
myField.focus();
//bottom.innerHTML = selectValue;
} else {
myField.value += myValue;
myField.focus();
// bottom.innerHTML = "3";
}
}