开发者

CKEditor on focus remove default value

开发者 https://www.devze.com 2023-03-29 09:10 出处:网络
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 defaul

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开发者_JAVA技巧', function()
{
    alert(1);
});


Have you tried this?

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号