TypeError:CKEDITOR.instance未定义 - 测试对象实例会出错

时间:2013-04-16 14:08:00

标签: javascript jquery ckeditor typeof

情况: 在jQuery对话框中调用CKEDITOR(这里有很多挑战)。 我的具体问题是,当我测试CKEDITOR实例的存在时,我总是会抛出一个错误。

浏览器测试:Firefox 21,IE 9,Safari 5.1.7

错误:错误:TypeError:CKEDITOR.instance未定义

所有这些测试CKEDITOR实例的方法总是抛出一个javascript错误:

if(!CKEDITOR.instance ['editor']){

如果(CKEDITOR.instance [ '编辑']){

if(!typeof CKEDITOR.instance ['editor'] ==“undefined”){

if(!typeof(CKEDITOR.instance ['editor'])==“undefined”){

if(typeof CKEDITOR.instance ['editor']!=“undefined”){

if(typeof CKEDITOR.instance ['editor']!==“undefined”){

if(typeof CKEDITOR.instance ['editor'] ===“undefined”){

当然,在发布此帖之前,我已经搜索了StackOverflow。我已尝试过在StackOverflow上建议的“typeof”和“undefined”的所有组合。

如果在实例存在的测试中遇到错误,我如何测试对象实例的存在?一个难题!

2 个答案:

答案 0 :(得分:1)

不会

CKEDITOR.instance

<强> BUT:

CKEDITOR.instances

它始终存在 - 没有必要对它进行测试。

答案 1 :(得分:0)

您正在测试错误的对象(在这种情况下是对象属性)。 instance未定义,因此在测试其中一个属性之前,您必须首先检查是否已定义instance

if ( CKEDITOR.instance && CKEDITOR.instance['editor'] ) {

或者如果你想知道什么时候没有定义,

if ( !CKEDITOR.instance || !CKEDITOR.instance['editor'] ) {