jquery 通过从文本区域阅读属性来创建html5占位符

wfveoks0  于 2022-11-03  发布在  jQuery
关注(0)|答案(9)|浏览(163)

对于标准的文本区域,我使用这个plugin来创建一个占位符。我如何扩展tinymce,使它也能以这种方式工作呢?
例如,从textarea属性读取默认值,然后在用户关注iframe时清除该值。
与CKEditor类似:http://alfonsoml.blogspot.com.es/2012/04/placeholder-text-in-ckeditor.html

cbjzeqam

cbjzeqam1#

我重构了Tom杜克的代码,以便使用其jquery插件处理TinyMCE4

$('textarea.tinymce').tinymce({
  script_url: _base + '/assets/js/tinymce/tinymce.min.js',
  theme: "modern",
  setup: function(editor) {
    // Set placeholder
    var placeholder = $('#' + editor.id).attr('placeholder');
    if (typeof placeholder !== 'undefined' && placeholder !== false) {
      var is_default = false;
      editor.on('init', function() {
        // get the current content
        var cont = editor.getContent();

        // If its empty and we have a placeholder set the value
        if (cont.length === 0) {
          editor.setContent(placeholder);
          // Get updated content
          cont = placeholder;
        }
        // convert to plain text and compare strings
        is_default = (cont == placeholder);

        // nothing to do
        if (!is_default) {
          return;
        }
      })
      .on('focus', function() {
        // replace the default content on focus if the same as original placeholder
        if (is_default) {
          editor.setContent('');
        }
      })
      .on('blur', function() {
        if (editor.getContent().length === 0) {
          editor.setContent(placeholder);
        }
      });
    }
  }
});
li9yvcax

li9yvcax2#

如果没有占位符属性,则会出现错误。
我把这个答案中的代码组合起来:jQuery hasAttr checking to see if there is an attribute on an element,以获取下面处理该场景的修改代码:

setup: function(ed) {

// Set placeholder
var tinymce_placeholder = $('#'+ed.id);
var attr = tinymce_placeholder.attr('placeholder');

// For some browsers, `attr` is undefined; for others,
// `attr` is false.  Check for both.
    if (typeof attr !== 'undefined' && attr !== false) {
        var is_default = false;

        ed.onInit.add(function(ed) {
            // get the current content
            var cont = ed.getContent();

            // If its empty and we have a placeholder set the value
            if(cont.length == 0){
                ed.setContent(tinymce_placeholder.attr("placeholder"));

                // Get updated content
                cont = tinymce_placeholder.attr("placeholder");
            }

            // convert to plain text and compare strings
            is_default = (cont == tinymce_placeholder.attr("placeholder"));

            // nothing to do
            if (!is_default){
                return;
            }
        });

        ed.onMouseDown.add(function(ed,e) {
            // replace the default content on focus if the same as original placeholder
            if (is_default){
                ed.setContent('');
            }
        });
    }
}
klsxnrf1

klsxnrf13#

上面的答案对我不起作用,但是下面是基于上面的答案并使用onchange_callback的我的(对我来说)工作代码。希望它能帮助别人!

onchange_callback : function(ed){
            var tinymce_placeholder = $('#' + ed.id).attr("placeholder");     
            setTimeout(function () {
                var content = ed.getContent().replace(/<p>|<\/p>/g, '');
                if (content == '') {
                    ed.setContent(tinymce_placeholder);
                }
            }, 200);
        },
        setup: function (ed) {
            // Set placeholder
            var tinymce_placeholder = $('#' + ed.id);
            if (tinymce_placeholder.length) {
                ed.onInit.add(function (ed) {
                    var cont = ed.getContent();
                    if (cont.length == 0) {
                        ed.setContent(tinymce_placeholder.attr("placeholder"));
                    }
                });
                ed.onMouseDown.add(function (ed, e) {
                    var content = ed.getContent().replace(/<p>|<\/p>/g, '');
                    if (content == tinymce_placeholder.attr("placeholder")) {
                        ed.setContent('');
                    }
                });
            }
        }
rwqw0loc

rwqw0loc4#

请尝试以下代码:
为tinyMCE内联编辑器添加占位符文本:

$scope.ContentOptions = {
    setup: function(editor) {

        editor.on('init', function () {
            // Default classes of tinyMCE are a bit weird
            // I add my own class on init
            // this also sets the empty class on the editor on init
            tinymce.DOM.addClass( editor.bodyElement, 'content-editor' );
        });

        // You CAN do it on 'change' event, but tinyMCE sets debouncing on that event
        // so for a tiny moment you would see the placeholder text and the text you you typed in the editor
        // the selectionchange event happens a lot more and with no debouncing, so in some situations
        // you might have to go back to the change event instead.
         editor.on('selectionchange', function () {
             if ( editor.getContent() === "" ) {
                 tinymce.DOM.addClass( editor.bodyElement, 'empty' );
             } else {
                 tinymce.DOM.removeClass( editor.bodyElement, 'empty' );
             }
         });
   }}

视图中的HTML部件

<div data-placeholder="Content..." id="post-content-editor" ui-tinymce="ContentOptions" ng-model="newPostContentModel"></div>

最后是创建占位符文本的CSS(它从data-placeholder=“Content...”中获取,但您可以直接在css中完成

.content-editorr:before {
        display: none;
}
.content-editor.empty:before {
        display: block;
        position: absolute;
        content: attr(data-placeholder);
}

我在github上找到的:
https://github.com/angular-ui/ui-tinymce/issues/197
我尝试了很多占位符解决方案,发现这个解决方案非常有用,因为它满足了占位符的所有要求。我认为这是最好的解决方案,

kuhbmx9i

kuhbmx9i5#

对于tinymce 4,我在没有定义艾德的情况下遇到了一些问题。tinymce 4使用ed.on('event ',callback)来代替。这是我的实现。我还使用了focus而不是mousedown作为清除编辑器时要监听的事件,因为mousdown由于某种原因不工作。

setup :  function(ed) {

        // Set placeholder
        var tinymce_placeholder = $('#'+ed.id);
        var attr = tinymce_placeholder.attr('placeholder');

        // For some browsers, `attr` is undefined; for others,
        // `attr` is false.  Check for both.
        if (typeof attr !== 'undefined' && attr !== false) {
            var is_default = false;

            ed.on('init' , function(ed) {
                // get the current content
                var cont = ed.target.getContent();

                // If its empty and we have a placeholder set the value
                if(cont.length == 0){
                    ed.target.setContent(tinymce_placeholder.attr("placeholder"));

                    // Get updated content
                    cont = tinymce_placeholder.attr("placeholder");
                }

                // convert to plain text and compare strings
                is_default = (cont == tinymce_placeholder.attr("placeholder"));

                // nothing to do
                if (!is_default){
                    return;
                }
            });

            ed.on('focus', function(ed,e) {
                // replace the default content on focus if the same as original placeholder
                if (is_default){
                    ed.target.setContent('');
                }
            });
        }
    }

我希望这对那些对其他答案有疑问的人有所帮助

vwkv1x7d

vwkv1x7d6#

我发现了另一种方法,它不会破坏文本区域的内容。我喜欢这种方法,这样我就不必设置特殊的条件来阻止用户提交只是占位符的内容。但我添加了onclick方法,以便标签是可单击的--如果没有这个方法,用户就必须在<label>的下面或旁边单击。这在TinyMCE 4中非常有效。

样式

.tinyMCEPlaceholder {
    top: 45px;
    left: 9px;
    z-index: 1;
    color: #bbbbbb;
    position: absolute;
    cursor:text;
}

html文件

<div id="holder">
    <label for="tinymce" onclick="tinymce.execCommand('mceFocus',false,'area_id');">Type your article</label>
    <textarea id="comments" id="area_id" class="tinymce" name="comments" cols="40" rows="10"></textarea>
</div>

<!-- or -->

<div id="holder">
    <label for="tinymce" onclick="tinymce.get('area_id').fire('focus',{},false);">Type your article</label>
    <textarea id="comments" id="area_id" class="tinymce" name="comments" cols="40" rows="10"></textarea>
</div>

TinyMCE安装程序

// setup
    setup : function(ed) {
        /* toggle all labels that have the attr 'tinymce' */
        ed.on('init', function() {
            if(ed.getContent() != '') {
                $('label[for="tinymce"]').hide();
            }

            $(ed.getDoc()).contents().find('body').focus(function(){
                $('label[for="tinymce"]').hide();
            });

            $(ed.getDoc()).contents().find('body').blur(function(){
                if(ed.getContent() == '') {
                    $('label[for="tinymce"]').show();
                }
            });
        });
    },
nwsw7zdq

nwsw7zdq7#

这个插件TinyMCE似乎是一个解决方案:
https://github.com/mohan/tinymce-placeholder
希望能有所帮助!

j8yoct9x

j8yoct9x8#

Gaurav的解决方案运行正常,但需要做一些修改。我使用了以下修改使其近乎完美:

editor.on("init", function(){
    tinymce.DOM.addClass( editor.bodyElement, 'content-editor' );
    if ( editor.getContent() === "" ) {
        tinymce.DOM.addClass( editor.bodyElement, 'empty' );
        var pseudoBeforeHeight = window.getComputedStyle(editor.bodyElement, ':before').height;
        tinymce.DOM.setStyles( editor.bodyElement, { 'height': pseudoBeforeHeight } );
    }
});
klh5stk1

klh5stk19#

我通过在textarea元素上添加data-mce-placeholder属性,轻松地添加了占位符。
data-mce-placeholder在编辑器iframe中的body::before文本css中引用

<textarea
  ng-model="textContent",
  ng-change="save()",
  style="height: 360px; max-height: 50vh; border: none !important",
  ui-tinymce="tinymceOptions"
  data-mce-placeholder="Input your text here"
></textarea>

相关问题