2011-08-21 51 views
2

对于一个正常的输入字段,我可以编写类似下面的代码,这将删除输入字段的默认值,当我点击,如果我不写任何东西在输入字段,比当我离开输入字段时,默认值将会返回。CKEditor on focus删除默认值

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

但我不知道如何用CKEditor做到这一点..我可以得到一些帮助。

我发现这个代码,当我点击CKEditor时会发出警报。但我不知道如何修改它以按我需要的方式工作。

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

回答

2

你试过这个吗?

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

this.value is undefined for me – Gowri

0

结合上面的想法[该另一SO文章] [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(''); 
} 

});

在测试之前,您可能不需要修剪文本。这使用getData来查找文本是什么,并使用setData来更改它。