2011-11-14 41 views
0

我正在使用ckeditor显示格式化文本。我正在使用Asp.net mvc 3剃刀辅助方法Html.CKEditorFor(x => x.Description)用于上述目的。需要实现这个ckeditor.I的 水印功能正在使用为目的下面的代码,焦点事件不使用CKEditor从文本框触发

$("#Description").focus(function() { 

    $(this).filter(function() { 

     // We only want this to apply if there's not 
     // something actually entered 
     return $(this).val() == "" || $(this).val() == "Type here" 

    }).removeClass("watermarkOn").val(""); 

});  

但是,当我专注于CKEditor的textbox.So我使用Firebug检查该文档的焦点事件不火,并发现默认文本呈现在由mvc ckeditor helper生成的以下类('cke_show_borders')中。因此,我将上面的代码修改为

$(".cke_show_borders").focus(function() { 
    $(this).filter(function() { 

     // We only want this to apply if there's not 
     // something actually entered 
     return $(this).val() == "" || $(this).val() == "Type here" 

    }).removeClass("watermarkOn").val(""); 

}); 

但仍然焦点事件没有解雇。 我要去哪里请帮忙。 谢谢。

回答

0

确保你已经把这个代码在$(document).ready处理程序,以确保在尝试订阅任何事件之前的DOM满载:

$(function() { 
    $('.cke_show_borders').focus(function() { 
     $(this).filter(function() { 
      // We only want this to apply if there's not 
      // something actually entered 
      return $(this).val() == '' || $(this).val() == 'Type here'; 
     }).removeClass('watermarkOn').val(''); 
    }); 
}); 
+0

大家的变薄一直不停在文档就绪功能中。 – user1045002

2

我最好的猜测是,你应该尝试这样做使用他们的 API而不是jQuery。你检查了他们的developer features

顺便说一句,我见过的人使用类似的改变CKEditor的默认行为:

var editor = CKEDITOR.replace('ckeditor'); 
editor.on('instanceReady', function(e) { 
    // customize stuff here 
    // this.document.$.childNodes[1].childNodes[1].style.styleName = 'value'; 
    editor.focus(); 
}); 

同样,你可以修改他们的onfocus事件,像这样:

editor.on('focus', function(e) { 
    // code here 
}); 
+0

嗨,我实现了上面的代码,但在mozilla firefox console.Error上出现以下错误:未捕获的异常:[CKEDITOR.editor.replace]未找到id或名称为“ckeditor”的元素。 – user1045002

+0

哦,是的,你将不得不将'ckeditor'更改为你给你的'textarea'标签的'id'。 –

+0

您好所有的东西都保存在文档就绪函数中,当遵循上面的方法时焦点事件被触发,但问题是我无法在return语句返回true时删除应用于ckeditor的类(watermarkOn)。 – user1045002

相关问题