国产清纯在线一区二区,日本强伦片中文字幕免费看,结衣波多野番号,亚洲欧美日韩精品色xxx,开心色怡人综合网站

返回上一頁 使用PHP請求ChatGPT聊天接口(gpt-3.5-turbo) 網(wǎng)站建設(shè)案例 杜絕站長統(tǒng)計(baidu、cnzz)數(shù)據(jù)被各種廣告騷擾

當(dāng)前位置:首頁 > 解決方案 > 網(wǎng)絡(luò)方案 > 詳細(xì)內(nèi)容

自定義開發(fā)富文本編輯器(Javascript實現(xiàn)點(diǎn)擊插入內(nèi)容到textarea光標(biāo)處)

時間:2021-04-09 瀏覽:3581次 + 打印

富文本編輯器相信很多程序員都用過,但是如何自己制作一個仿富文本的編輯器來解決一些簡單的或自定義的需求呢?下面給大家共享一個使用JavaScript實現(xiàn)在textarea光標(biāo)處插入指定文本內(nèi)容以及字符串。

點(diǎn)擊按鈕,可以把按鈕的內(nèi)容插入到textarea文本框內(nèi)光標(biāo)處,光標(biāo)默認(rèn)在文本框開頭。

通過其他的js就可以實現(xiàn)文本框插入表情、選中文字加粗、內(nèi)容中插入圖片等等。

HTML代碼:

<form id="form1" name="form1" method="post" action="">
    <label>
        <textarea name="text" id="text" cols="45" rows="10">
        這是測試內(nèi)容,請在任意位置插入內(nèi)容。
        這是測試內(nèi)容,請在任意位置插入內(nèi)容。
        這是測試內(nèi)容,請在任意位置插入內(nèi)容。
        這是測試內(nèi)容,請在任意位置插入內(nèi)容。
        這是測試內(nèi)容,請在任意位置插入內(nèi)容。
        這是測試內(nèi)容,請在任意位置插入內(nèi)容。
        這是測試內(nèi)容,請在任意位置插入內(nèi)容。
        這是測試內(nèi)容,請在任意位置插入內(nèi)容。
        這是測試內(nèi)容,請在任意位置插入內(nèi)容。
        這是測試內(nèi)容,請在任意位置插入內(nèi)容。
        這是測試內(nèi)容,請在任意位置插入內(nèi)容。
        這是測試內(nèi)容,請在任意位置插入內(nèi)容。
        </textarea>

        <a id="insert" href="javascript:void(0);">{PHP code here}</a>
    </label>
</form>

Javascript代碼:

<script type="text/javascript">
    var text = document.getElementById('text');
    var insert = document.getElementById('insert');
    insert.onclick = function () {
        insertAtCursor(text, this.innerHTML);
    };

    function insertAtCursor(myField, myValue) {
        if (document.selection) {
            //IE support
            myField.focus();
            sel = document.selection.createRange();
            sel.text = myValue;
            sel.select();
        } 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);

            myField.value = beforeValue + myValue + afterValue;

            myField.selectionStart = startPos + myValue.length;
            myField.selectionEnd = startPos + myValue.length;
            myField.focus();
        } else {
            myField.value += myValue;
            myField.focus();
        }
    }
</script>
網(wǎng)站建設(shè)公司項目經(jīng)理

掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音
解答:網(wǎng)站優(yōu)化、網(wǎng)站建設(shè)、APP開發(fā)、小程序開發(fā)

藤設(shè)計是一家以提供網(wǎng)站建設(shè)網(wǎng)站優(yōu)化、APP開發(fā)、小程序開發(fā)、網(wǎng)絡(luò)營銷推廣為主的互聯(lián)網(wǎng)開發(fā)公司。以客戶需求為導(dǎo)向,客戶利益為出發(fā)點(diǎn),結(jié)合自身設(shè)計及專業(yè)建站優(yōu)勢,為客戶提供從基礎(chǔ)建設(shè)到營銷推廣的一整套解決方案,探索并實現(xiàn)客戶商業(yè)價值較大化,為所有謀求長遠(yuǎn)發(fā)展的企業(yè)貢獻(xiàn)全部力量。

Learn more

Our Service 上海網(wǎng)站建設(shè)
QQ客服 微信客服 返回頂部
網(wǎng)站制作
掃二維碼與項目經(jīng)理溝通
×