javascript Summernote:初始化后设置焦点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26538698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:06:56  来源:igfitidea点击:

Summernote: set focus after initialization

javascriptjquerysummernote

提问by lsouza

Summernotegives you the option of giving focus to the editor when you create it, passing this options:

Summernote为您提供了在创建编辑器时为其提供焦点的选项,传递以下选项:

$('#summernote').summernote({
     focus: true
});

But after initialization, it seems you can't give focus to the textarea by clicking a button or similar. I've tried several ways, without success.

但是初始化后,似乎您无法通过单击按钮或类似按钮将焦点放在 textarea 上。我尝试了几种方法,都没有成功。

Anyone did that?

有人这样做过吗?

采纳答案by lsouza

After coming back to this problem and trying a lot of solutions, I came to one that works:

在回到这个问题并尝试了很多解决方案之后,我找到了一个有效的方法:

$('.note-editable').trigger('focus');

$('.note-editable').trigger('focus');

Triggering the event through jQuery works, but using the focus()function doesn't.

通过 jQuery 触发事件有效,但使用该focus()函数则无效。

回答by ice

$('.summernote').summernote('focus')

ps. i want to find an angular way out .. and it works

附:我想找到一个有角度的出路..它有效

回答by leo

$(document).ready(function () {
                        $.fn.extend({
                            placeCursorAtEnd: function () {
                                // Places the cursor at the end of a contenteditable container (should also work for textarea / input)
                                if (this.length === 0) {
                                    throw new Error("Cannot manipulate an element if there is no element!");
                                }
                                var el = this[0];
                                var range = document.createRange();
                                var sel = window.getSelection();
                                var childLength = el.childNodes.length;
                                if (childLength > 0) {
                                    var lastNode = el.childNodes[childLength - 1];
                                    var lastNodeChildren = lastNode.childNodes.length;
                                    range.setStart(lastNode, lastNodeChildren);
                                    range.collapse(true);
                                    sel.removeAllRanges();
                                    sel.addRange(range);
                                }
                                return this;
                            }
                        });
                    });

then:

然后:

$('.note-editable').click(function(){
                                  //$('#summernote').summernote('focus');
                                  $(this).placeCursorAtEnd();
                                });

①focus on click or tap ②focus at the end of content

①关注点击或点击 ②关注内容末尾

it works on mobile device too

它也适用于移动设备

回答by Manish

please refer codepenfor this problem

这个问题请参考codepen

/* Summernote Validation */

/* Summernote 验证 */

$(function () {
    var summernoteForm = $('.form-validate-summernote');
    var summernoteElement = $('.summernote');

    var summernoteValidator = summernoteForm.validate({
        errorElement: "div",
        errorClass: 'is-invalid',
        validClass: 'is-valid',
        ignore: ':hidden:not(.summernote),.note-editable.card-block',
        errorPlacement: function (error, element) {
            // Add the `help-block` class to the error element
            error.addClass("invalid-feedback");
            console.log(element);
            if (element.prop("type") === "checkbox") {
                error.insertAfter(element.siblings("label"));
            } else if (element.hasClass("summernote")) {
                error.insertAfter(element.siblings(".note-editor"));
            } else {
                error.insertAfter(element);
            }
        }
    });

    summernoteElement.summernote({
        height: 300,
        callbacks: {
            onChange: function (contents, $editable) {
                // Note that at this point, the value of the `textarea` is not the same as the one
                // you entered into the summernote editor, so you have to set it yourself to make
                // the validation consistent and in sync with the value.
                summernoteElement.val(summernoteElement.summernote('isEmpty') ? "" : contents);

                // You should re-validate your element after change, because the plugin will have
                // no way to know that the value of your `textarea` has been changed if the change
                // was done programmatically.
                summernoteValidator.element(summernoteElement);
            }
        }
    });

});

回答by Mohamed Saad

<div class="col-lg-12 col-md-6 col-xs-12">
    <div class="form-group">
        <label>  Skills</label>
        <span ng-messages="EditForm.Skills.$error" ng-if="EditForm.Skills.$dirty || isEditFormSubmitted ">
            <span class="text-danger" ng-message="required" ng-bind="error msg"></span>
        </span>
        <text-angular id="Skills" name="Skills" ng-model="EditVacancyModel.Skills" ta-toolbar="[['bold','underline','italics','ul','ol']]"ng-required="true"></text-angular>
    </div>
</div>

回答by mxro

You can set the focus by putting the focus on the editable divused by Summernote:

您可以通过将焦点放在Summernote 使用的可编辑 div上来设置焦点:

document.querySelectorAll(".note-editable").focus();

or using JQuery

或使用 JQuery

$('#summernote').find('.note-editable').focus();