Use Javascript variable in object name? [duplicate]

北城以北 提交于 2019-11-26 21:43:16

问题


I am using CKEditor, and, when referring to the CKEditor instance, I need to use a variable. But, since calling the instance is a object, I am really not sure how to do this.

I am using:

CKEDITOR.instances.textarea123.insertHtml('<p>Whatever</p>');

The issue is, I need 123 to be a variable, because I need to change the instance based on the editor page that is loaded.

So, how can I use a variable in an object name?

For obvious reasons the following does not work, but I need to achieve what it is "pretending" to do:

var id = 354;
CKEDITOR.instances.textarea+id+.insertHtml('<p>Whatever</p>');

回答1:


Try the following:

var id = 354;
CKEDITOR.instances['textarea'+id].insertHtml('<p>Whatever</p>');



回答2:


You can use array notation:

CKEDITOR.instances['textarea' + id].insertHtml('<p>Whatever</p>');



回答3:


var id = 354;
CKEDITOR.instances["textarea" + id].insertHtml('<p>Whatever</p>');

Since instances is an object, and objects essentially are hash tables you can access them with the array notation.



来源:https://stackoverflow.com/questions/1567278/use-javascript-variable-in-object-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!