将标签插入文本区域的按钮

发布于 2024-11-30 13:55:12 字数 187 浏览 0 评论 0原文

我相信这个问题之前已经被回答过,但是我怎样才能拥有按钮/图像/任何东西,以便当我单击它们时,它会像您找到的那样在光标位置周围插入 在论坛中(甚至 StackOverflow 提出问题页面)?
再说一次,我知道这个问题已经得到解答,但我能找到的只是使用 jQuery,而我的服务器不支持它......
谢谢

I believe this has been answered before but how can I have buttons / images / whatever so that when I click them it inserts the <b></b> around the cursor position like what you find in forums (and even the StackOverflow ask a question page)?
Again, I know this has been answered but all I could find was using jQuery and my server does not support it...
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

镜花水月 2024-12-07 13:55:12

这是我不久前写的jsFiddle

    function boldText(textAreaId, link) 
    {
        var browser=navigator.appName
        var b_version=navigator.appVersion

        if (browser=="Microsoft Internet Explorer" && b_version>='4')
        {
            var str = document.selection.createRange().text;
            document.getElementById(textAreaId).focus();
            var sel = document.selection.createRange();
            sel.text = "<b>" + str + "</b>";
            return;
        }

        field = document.getElementById(textAreaId);
        startPos = field.selectionStart;
        endPos = field.selectionEnd;
        before = field.value.substr(0, startPos);
        selected = field.value.substr(field.selectionStart, (field.selectionEnd - field.selectionStart));
        after = field.value.substr(field.selectionEnd, (field.value.length - field.selectionEnd));
        field.value = before + "<b>" + selected + "</b>" + after;
    }

Here's one I wrote awhile ago jsFiddle

    function boldText(textAreaId, link) 
    {
        var browser=navigator.appName
        var b_version=navigator.appVersion

        if (browser=="Microsoft Internet Explorer" && b_version>='4')
        {
            var str = document.selection.createRange().text;
            document.getElementById(textAreaId).focus();
            var sel = document.selection.createRange();
            sel.text = "<b>" + str + "</b>";
            return;
        }

        field = document.getElementById(textAreaId);
        startPos = field.selectionStart;
        endPos = field.selectionEnd;
        before = field.value.substr(0, startPos);
        selected = field.value.substr(field.selectionStart, (field.selectionEnd - field.selectionStart));
        after = field.value.substr(field.selectionEnd, (field.value.length - field.selectionEnd));
        field.value = before + "<b>" + selected + "</b>" + after;
    }
冷默言语 2024-12-07 13:55:12

这是一个修改版本,您可以输入任何标签字母

function TextMod(textAreaId,input) {
        var browser=navigator.appName
        var b_version=navigator.appVersion

        if (browser=="Microsoft Internet Explorer" && b_version>='4')
        {
            var str = document.selection.createRange().text;
            document.getElementById(textAreaId).focus();
            var sel = document.selection.createRange();
            if(input == "br"){
                sel.text =  str + "</"+input+">";
            }
            else{
            sel.text = "<"+input+">" + str + "</"+input+">";
            }
            return;
        }

        field = document.getElementById(textAreaId);
        startPos = field.selectionStart;
        endPos = field.selectionEnd;
        before = field.value.substr(0, startPos);
        selected = field.value.substr(field.selectionStart, (field.selectionEnd - field.selectionStart));
        after = field.value.substr(field.selectionEnd, (field.value.length - field.selectionEnd));
        if(input == "br"){
            field.value = before  + selected + "</"+input+">" + after;
        }
        else{
            field.value = before + "<"+input+">" + selected + "</"+input+">" + after;
        }

    }

here is a modified version that you can let input any tag letter

function TextMod(textAreaId,input) {
        var browser=navigator.appName
        var b_version=navigator.appVersion

        if (browser=="Microsoft Internet Explorer" && b_version>='4')
        {
            var str = document.selection.createRange().text;
            document.getElementById(textAreaId).focus();
            var sel = document.selection.createRange();
            if(input == "br"){
                sel.text =  str + "</"+input+">";
            }
            else{
            sel.text = "<"+input+">" + str + "</"+input+">";
            }
            return;
        }

        field = document.getElementById(textAreaId);
        startPos = field.selectionStart;
        endPos = field.selectionEnd;
        before = field.value.substr(0, startPos);
        selected = field.value.substr(field.selectionStart, (field.selectionEnd - field.selectionStart));
        after = field.value.substr(field.selectionEnd, (field.value.length - field.selectionEnd));
        if(input == "br"){
            field.value = before  + selected + "</"+input+">" + after;
        }
        else{
            field.value = before + "<"+input+">" + selected + "</"+input+">" + after;
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文