不确定我想要实现的目标中的错误在哪里。
我正在使用其他人的代码,不幸的是他们在某些地方使用了数字作为 div id。
这些数字 ID 被用在不同的地方,如果可以的话,我想找到一种方法来保持原样。
所以,
以 JSON 格式返回以下内容:
editorID: "1000"
在我的 AJAX 调用中,我使用该返回,如下所示:
var editorID = response.editorID;
CKEDITOR.instances.editorID.insertHtml('<br><img class="buildimage" src="http://www.buildsanctuary.com/phpLibs/bulletproof-master/src/userBuildImages/'+response.imageName+'"><br>');
但是这给了我一个错误,说 editorID 未定义。
正如你所见,我已经在代码中使用了 JSON 响应,这工作正常,所以它不是数据类型等的问题。
我也尝试过这样做:
alert(response.editorID);
这给了我正确的值。
当我尝试将数字直接放入 CKEditor insertHTML 代码时,它显示了我的语法错误,所以也许这就是问题所在。如果是这样,有什么解决办法吗?
谢谢。克雷格。
最佳答案
要将变量用作属性,您必须使用 []
表示法:
CKEDITOR.instances[editorID].insertHtml('<br><img class="buildimage" src="http://www.buildsanctuary.com/phpLibs/bulletproof-master/src/userBuildImages/'+response.imageName+'"><br>');
当您使用.editorID
时,它会查找名为editorID
的属性,而不是1000
。
当属性不是有效标识符时,您还必须使用此语法。所以如果你想直接输入数字,你可以这样写:
CKEDITOR.instances['1000'].insertHtml('<br><img class="buildimage" src="http://www.buildsanctuary.com/phpLibs/bulletproof-master/src/userBuildImages/'+response.imageName+'"><br>');
关于javascript - 当值为数字时,JSON 返回未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25899329/