【问题标题】:TypeError: CKEDITOR.instance is undefined -- Test for object instance gives errorTypeError: CKEDITOR.instance 未定义——测试对象实例给出错误
【发布时间】:2013-04-16 14:08:00
【问题描述】:

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

经过测试的浏览器:Firefox 21、IE 9、Safari 5.1.7

错误:Error: TypeError: CKEDITOR.instance is undefined

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

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

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

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”的所有组合。

如果在测试实例存在时出错,我该如何测试对象实例的存在?一个难题!

【问题讨论】:

  • if(typeof CKEDITOR.instance === "undefined"){

标签: javascript jquery ckeditor typeof


【解决方案1】:

CKEDITOR.instance

但是:

CKEDITOR.instances

而且它始终存在 - 无需对其进行测试。

【讨论】:

    【解决方案2】:

    您正在测试错误的对象(或本例中的对象属性)。 instance 未定义,因此您必须先检查 instance 是否已定义,然后再测试其中一个属性。

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

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

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

    【讨论】:

    • 好吧,见鬼!谢谢凯文 B。
    猜你喜欢
    • 1970-01-01
    • 2012-07-23
    • 2020-06-12
    • 1970-01-01
    • 2022-07-02
    • 2016-10-13
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多