return focus to contenteditable after execCommand?

前端 未结 5 1837
臣服心动
臣服心动 2021-02-04 08:56

I have the following code demonstrating contenteditable property and a button that will inject bold text into the paragraph with contenteditable area. My question is how to retu

5条回答
  •  甜味超标
    2021-02-04 09:35

    YOU SHOULD USE .contents()

    var current;
    $(function(){
        $("p[contenteditable]").focus(function() {
            current = this;
        });
    
        $('#bold').click(function (){
                document.execCommand('bold', false, true);
                $(current).contents().focus();
        });
    });
    

提交回复
热议问题