CKEditor on focus remove default value

萝らか妹 提交于 2019-12-24 05:23:14

问题


For a normal input field i can write something like the below code, which will remove the default value of an input field when i click, and if i dont write anything in the input field, than the default value will return when i leave the input field.

jQuery('input[type="text"]').focus(function()
    {
        if (this.value == this.defaultValue)
        {
            this.value = '';
        }
        if(this.value != this.defaultValue)
        {
            this.select();
        }
    });

    jQuery('input[type="text"]').blur(function()
    {
        if (this.value == '')
        {
            this.value = this.defaultValue;
        }
    });

But i have no clue how to do this with CKEditor.. Can I get some help.

I found this code which will alert when I click in the CKEditor. But I dont know how modify it to work the way I need.

CKEDITOR.instances['post_content'].on('focus', function()
{
    alert(1);
});

回答1:


Have you tried this?

CKEDITOR.instances['post_content'].on('focus', function()
{
    if (this.value == defaultValue)
    {
        this.value = '';
    }
});



回答2:


Combining the idea above with [this other SO article][1]:

// delete default text on focus
CKEDITOR.instances['editor1'].on('focus', function () {

    var defaultText = '<p class=\"ckNormal\">Type your comment here</p>';
    var currentText = CKEDITOR.instances.editor1.getData();

    // debug - removed
    // alert(defaultText + '\n' + currentText);

    if (defaultText.trim()==currentText.trim()) {
        CKEDITOR.instances.editor1.setData('');
    }
});

You probably won't need to trim the text before testing. This uses getData to find what the text is, and setData to change it.



来源:https://stackoverflow.com/questions/7137958/ckeditor-on-focus-remove-default-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!