在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ contenteditable="true" 插入表情,表

contenteditable="true" 插入表情,表情一直在最前邊,怎么讓表情插入在之前光標(biāo)定位的地方?

圖片描述

這段代碼是從網(wǎng)上摘的,但是好像輸入框失去焦點(diǎn)就會出問題,不用iframe的話怎么解決,請大神指點(diǎn)


//鎖定編輯器中鼠標(biāo)光標(biāo)位置。。
    var _insertimg = function(str) {
        var selection = window.getSelection ? window.getSelection() : document.selection;

        var range = selection.createRange ? selection.createRange() : selection.getRangeAt(0);

        if (!window.getSelection) {

            document.getElementById('sendInput').focus();

            var selection = window.getSelection ? window.getSelection() : document.selection;

            var range = selection.createRange ? selection.createRange() : selection.getRangeAt(0);

            range.pasteHTML(str);

            range.collapse(false);

            range.select();

        } else {

            document.getElementById('sendInput').focus();

            range.collapse(false);

            var hasR = range.createContextualFragment(str);

            var hasR_lastChild = hasR.lastChild;

            while (hasR_lastChild && hasR_lastChild.nodeName.toLowerCase() == "br" && hasR_lastChild.previousSibling && hasR_lastChild.previousSibling.nodeName.toLowerCase() == "br") {

                var e = hasR_lastChild;

                hasR_lastChild = hasR_lastChild.previousSibling;

                hasR.removeChild(e)

            }

            range.insertNode(hasR);

            if (hasR_lastChild) {

                range.setEndAfter(hasR_lastChild);

                range.setStartAfter(hasR_lastChild)

            }

            selection.removeAllRanges();

            selection.addRange(range)

        }
    }
回答
編輯回答
涼汐

之前表情列表用的是li標(biāo)簽,點(diǎn)擊li標(biāo)簽div貌似會失去焦點(diǎn),插入就會出問題,在li標(biāo)簽里插入input標(biāo)簽,讓點(diǎn)擊時(shí)間在input標(biāo)簽上觸發(fā)就不會失去焦點(diǎn),解決。 但感覺這個(gè)方法不太好,麻煩

2017年2月13日 12:52