Summernote:初始化后设置焦点

时间:2014-10-23 22:41:20

标签: javascript jquery summernote

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

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

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

有人这样做过吗?

6 个答案:

答案 0 :(得分:6)

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

PS。我想找到一个有条不紊的出路..它的工作原理

答案 1 :(得分:3)

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

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

通过jQuery触发事件有效,但使用focus()函数不会。

答案 2 :(得分:1)

$(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;
                            }
                        });
                    });

然后:

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

①专注于点击或点按 ②焦点在内容的末尾

它也适用于移动设备

答案 3 :(得分:0)

请参阅codepen 对于这个问题

  

/ * 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);
            }
        }
    });

});

答案 4 :(得分:0)

<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>

答案 5 :(得分:-2)

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

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

或使用JQuery

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