Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript生成CKEDITOR对for循环语句不起作用_Javascript_For Loop - Fatal编程技术网

使用javascript生成CKEDITOR对for循环语句不起作用

使用javascript生成CKEDITOR对for循环语句不起作用,javascript,for-loop,Javascript,For Loop,是否可以在javascript中使用for循环语句创建ckeditor?每次我在textarea中生成一个ckeditor时,该ckeditor都不会显示。任何想法。多谢各位 <script type="text/javascript"> for(var index = 1; index <= 4; index++ ){ str += '<textarea class="ckeditor" name="answer[]"></textarea>';

是否可以在javascript中使用for循环语句创建ckeditor?每次我在textarea中生成一个ckeditor时,该ckeditor都不会显示。任何想法。多谢各位

<script type="text/javascript">
 for(var index = 1; index <= 4; index++ ){
  str += '<textarea class="ckeditor" name="answer[]"></textarea>';
 }
</script>
示例图像:

查看,似乎需要插入HTML textareas,然后为每个textarea调用create方法,并将其id作为参数传递。我从自述中提取了以下内容:

在HTML页面中添加CKEditor应替换的元素:

<textarea name="content" id="editor"></textarea>
加载经典编辑器版本,您可以选择CDN、npm和zip下载:

<script src="https://cdn.ckeditor.com/ckeditor5/<version>/classic/ckeditor.js"></script>
调用ClassicEditor.create方法:

<script> 
   ClassicEditor .create(
      document.querySelector( '#editor' )
   ) .catch( error => { console.error( error ); } );
</script>
你准备好了

因此,按照这些说明,您应该能够为循环中的每个textarea调用create,只要在调用create之前已将其添加到文档中


编辑:如果您真的想使用旧版本的ckeditor,它看起来像是提到使用.replace或.replaceClass

我尝试了您的答案,但这不起作用。textarea上的ID不是个好主意,因为您使用具有唯一ID的for循环生成了一个textarea,所以我尝试使用一个类而不是textarea上的ID,但不起作用。顺便说一下,谢谢你的帮助responding@YadYoung以下问题的选定答案建议使用appendChild,以便将新元素添加到DOM中,以及其他一些好的建议,以确保您可以访问动态添加的元素:bro,您是否能够解决此问题?我已经坚持了几个小时了: