在文本框中突出显示前缀/后缀的文本

Prefix/Suffix highlighted text in textbox

本文关键字:文本 前缀 显示 后缀      更新时间:2023-09-26

我必须制作一个小的javascript函数,为文本框中的选定文本添加前缀和后缀。

这是我目前为止写的:

function AddTags(name, prefix, suffix) {
    try
    {
        var textArea = document.getElementById(name).value;
        var i = 0;
        var textArray = textArea.split("'n");
        if (textArray == null) {
            document.getElementById(name).value += prefix + suffix
        }
        else {
            for (i = 0; i < textArray.length; i++) {
                textArray[i] = prefix + textArray[i] + suffix;
            }
            document.getElementById(name).value = textArray.join("'n");
        }
    }
    catch (err) { }
}

现在这个函数将提供的前缀和后缀添加到每一行,但我需要找出如何在Text before selection, Selected textText after selection中分解我的文本框的文本。

谁有这方面的经验?编辑:

TriniBoy的功能让我走上了正确的道路。我不需要整个建议。这是我的原始代码的编辑版本:

    function AddTags(name, prefix, suffix) {
    try
    {
        var textArea = document.getElementById(name);
        var i = 0;
        var selStart = textArea.selectionStart;
        var selEnd = textArea.selectionEnd;
        var textbefore = textArea.value.substring(0, selStart);
        var selected = textArea.value.substring(selStart, selEnd);
        var textAfter = textArea.value.substring(selEnd);
        if (textAfter == "") {
            document.getElementById(name).value += prefix + suffix
        }
        else {
            document.getElementById(name).value = textbefore + prefix + selected + suffix + textAfter;
        }
    }
    catch (err) { }
}

谢谢TriniBoy,我会把你的腿标记为答案。

根据您的演示和您的解释,希望我正确理解了您的要求。

请参阅代码注释了解详细信息。

查看此处的演示文件

var PreSuffApp = PreSuffApp || {
selText: "",
selStart: 0,
selEnd: 0,
getSelectedText: function (id) {
    var text = "",
        docSel = document.selection, //For IE
        winSel = window.getSelection,
        P = PreSuffApp,
        textArea = document.getElementById(id);
    if (typeof winSel !== "undefined") {
        text = winSel().toString(); //Grab the current selected text
        if (typeof docSel !== "undefined" && docSel.type === "Text") {
            text = docSel.createRange().text; //Grab the current selected text
        }
    }
    P.selStart = textArea.selectionStart; //Get the start of the selection range
    P.selEnd = textArea.selectionEnd; //Get the end of the selection range
    P.selText = text; //Set the value of the current selected text
},
addTags: function (id, prefix, suffix) {
    try {
        var textArea = document.getElementById(id),
            P = PreSuffApp,
            range = P.selEnd - P.selStart; //Used to calculate the lenght of the selection
        //Check to see if some valuable text is selected
        if (P.selText.trim() !== "") {
            textArea.value = textArea.value.splice(P.selStart, range, prefix + P.selText + suffix); //Call the splice method on your text area value
        } else {
            alert("You've selected a bunch of nothingness");
        }
    } catch (err) {}
}
};
//Extend the string obj to splice the string from a start character index to an end range, like an array.
String.prototype.splice = function (index, rem, s) {
    return (this.slice(0, index) + s + this.slice(index + Math.abs(rem)));
};