return focus to contenteditable after execCommand?

前端 未结 5 1776
臣服心动
臣服心动 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:17

    You may want to store the last clicked item in a variable and then call .focus() on it after the .execCommand has been executed. Something like this, I guess:

     $(function(){
            $('p.editable').click(function() {
                var clickedItem = $(this);
                clickedItem.attr('contenteditable', true).focus();
                $('#bold').click(function (){
                        document.execCommand('bold', false, true);
                        clickedItem.focus();
                });
            });
        });
    

    This way you can also remove the "contenteditable" attribute from the markup...

    HTH

提交回复
热议问题