javascript - 动态页面中的CK编辑器

标签 javascript ckeditor

我想在动态加载的加载页面中显示一个 Ck 编辑器,我使用了以下脚本,但是只有一个 ck 编辑器出现,其他编辑器看起来像文本区域?

     <script >
            function refer(format, id)
            {

            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                req=new XMLHttpRequest();

            }
            else
            {// code for IE6, IE5
                req=new ActiveXObject("Microsoft.XMLHTTP");

            }
            var strURL="referformat.php?format="+format+"&id="+id;

            if (req)
            {
                req.onreadystatechange = function() {



                    if (req.readyState == 4) {
                        // only if "OK"

                        if (req.status == 200)
                        {

                            //var a=document.getElementById('editor1').value="";
                            // alert(a);

                            // alert('i am here');
                            // CKEDITOR.instances.editor1.setData(req.responseText);
                                document.getElementById('refformat').innerHTML=req.responseText;





                CKEDITOR.replace( 'editor1',
                    {

                        fontSize_sizes : "8px;9px;10px;12px;14px;16px;18px;20px;22px;24px;26px;28px;30px;32px;34px",
                        toolbar :
                        [
                            ['Bold', 'Italic','Underline'],
                            ['Source', '-', 'Undo','Redo'],
                            ['Format','Font','FontSize'],
                            ['TextColor','BGColor'],
                            ['NumberedList','BulletedList','-','Blockquote'],
                            ['Link', 'Unlink','SpecialChar'],
                        ],
                        // Strip CKEditor smileys to those commonly used in BBCode.
                        smiley_images :
                        [
                            'regular_smile.gif','sad_smile.gif','wink_smile.gif','teeth_smile.gif','tounge_smile.gif',
                            'embaressed_smile.gif','omg_smile.gif','whatchutalkingabout_smile.gif','angel_smile.gif','shades_smile.gif',
                            'cry_smile.gif','kiss.gif'
                        ],
                        smiley_descriptions :
                        [
                            'smiley', 'sad', 'wink', 'laugh', 'cheeky', 'blush', 'surprise',
                            'indecision', 'angel', 'cool', 'crying', 'kiss'
                        ]
                } );

                CKEDITOR.replace( 'editor2', 
                    {

                        fontSize_sizes : "8px;9px;10px;12px;14px;16px;18px;20px;22px;24px;26px;28px;30px;32px;34px",
                        toolbar :
                        [
                            ['Bold', 'Italic','Underline'],
                            ['Source', '-', 'Undo','Redo'],
                            ['Format','Font','FontSize'],
                            ['TextColor','BGColor'],
                            ['NumberedList','BulletedList','-','Blockquote'],
                            ['Link', 'Unlink','SpecialChar'],
                        ],
                        // Strip CKEditor smileys to those commonly used in BBCode.
                        smiley_images :
                        [
                            'regular_smile.gif','sad_smile.gif','wink_smile.gif','teeth_smile.gif','tounge_smile.gif',
                            'embaressed_smile.gif','omg_smile.gif','whatchutalkingabout_smile.gif','angel_smile.gif','shades_smile.gif',
                            'cry_smile.gif','kiss.gif'
                        ],
                        smiley_descriptions :
                        [
                            'smiley', 'sad', 'wink', 'laugh', 'cheeky', 'blush', 'surprise',
                            'indecision', 'angel', 'cool', 'crying', 'kiss'
                        ]
                } );

                    CKEDITOR.replace( 'editor4', 
                    {

                        fontSize_sizes : "8px;9px;10px;12px;14px;16px;18px;20px;22px;24px;26px;28px;30px;32px;34px",
                        toolbar :
                        [
                            ['Bold', 'Italic','Underline'],
                            ['Source', '-', 'Undo','Redo'],
                            ['Format','Font','FontSize'],
                            ['TextColor','BGColor'],
                            ['NumberedList','BulletedList','-','Blockquote'],
                            ['Link', 'Unlink','SpecialChar'],
                        ],
                        // Strip CKEditor smileys to those commonly used in BBCode.
                        smiley_images :
                        [
                            'regular_smile.gif','sad_smile.gif','wink_smile.gif','teeth_smile.gif','tounge_smile.gif',
                            'embaressed_smile.gif','omg_smile.gif','whatchutalkingabout_smile.gif','angel_smile.gif','shades_smile.gif',
                            'cry_smile.gif','kiss.gif'
                        ],
                        smiley_descriptions :
                        [
                            'smiley', 'sad', 'wink', 'laugh', 'cheeky', 'blush', 'surprise',
                            'indecision', 'angel', 'cool', 'crying', 'kiss'
                        ]
                } );


                CKEDITOR.replace( 'editor3', 
                    {

                        fontSize_sizes : "8px;9px;10px;12px;14px;16px;18px;20px;22px;24px;26px;28px;30px;32px;34px",
                        toolbar :
                        [
                            ['Bold', 'Italic','Underline'],
                            ['Source', '-', 'Undo','Redo'],
                            ['Format','Font','FontSize'],
                            ['TextColor','BGColor'],
                            ['NumberedList','BulletedList','-','Blockquote'],
                            ['Link', 'Unlink','SpecialChar'],
                        ],
                        // Strip CKEditor smileys to those commonly used in BBCode.
                        smiley_images :
                        [
                            'regular_smile.gif','sad_smile.gif','wink_smile.gif','teeth_smile.gif','tounge_smile.gif',
                            'embaressed_smile.gif','omg_smile.gif','whatchutalkingabout_smile.gif','angel_smile.gif','shades_smile.gif',
                            'cry_smile.gif','kiss.gif'
                        ],
                        smiley_descriptions :
                        [
                            'smiley', 'sad', 'wink', 'laugh', 'cheeky', 'blush', 'surprise',
                            'indecision', 'angel', 'cool', 'crying', 'kiss'
                        ]
                } );

                //]]>





                        } else {
                            alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                        }
                    }
                }       
                req.open("GET", strURL, true);
                req.send(null);
            }
        }

 </script>

使用这个,我想显示3个ckeditor,但是ckeditor仅适用于第一个文本区域,其余2个文本区域ckeditor不起作用,我该如何解决这个问题

外部页面的 HTML 是

f(strtolower(str_replace(' ','',$reffername))=='injuryqedltd' && $format=='ia_format' )
                        {
                            ?>
                            <table>
                            <tr>
                            <td>

                                <strong>Findings on Initial Examination</strong>
                            </td>
                            <td>
                            <textarea  cols="80" rows="10" name="editor2" id="editor2" class="required textarea" title="Enter the Message"><?php echo $result[0]['Initialexam'];?></textarea>


                            </td>
                        </tr>

                         <tr>
                            <td>

                                <strong>Details of Treatment to be Provided</strong>
                            </td>
                            <td>
                            <textarea  cols="80" rows="10" name="editor3" id="editor3" class="required textarea" title="Enter the Message"><?php echo $result[0]['Detailtreatment'];?></textarea>


                            </td>
                        </tr>
                        <tr>
                            <td>

                                <strong>Benefit of Treatment to be Provided</strong>
                            </td>
                            <td>
                            <textarea  cols="80" rows="10" name="editor4" id="editor4" class="required textarea" title="Enter the Message"><?php echo $result[0]['BeTreatment'];?></textarea>


                            </td>
                        </tr>
                        </table>

                <?php       }
                 else if(strtolower(str_replace(' ','',$reffername))=='injuryqedltd' && $format=='fa_format' ) {


                     ?>
                            <table>
                            <tr>
                            <td>

                                <strong>Details of Treatment Provided</strong>
                            </td>
                            <td>
                            <textarea  cols="80" rows="10" name="editor5" id="editor5" class="required textarea" title="Enter the Message"><?php echo $result[0]['Initialexam'];?></textarea>


                            </td>
                        </tr>

                         <tr>
                            <td>

                                <strong>Benefit of Treatment Provided</strong>
                            </td>
                            <td>
                            <textarea  cols="80" rows="10" name="editor6" id="editor6" class="required textarea" title="Enter the Message"><?php echo $result[0]['Detailtreatment'];?></textarea>


                            </td>
                        </tr>
                        <tr>
                            <td>

                                <strong>Discharge Summary</strong>
                            </td>
                            <td>
                            <textarea  cols="80" rows="10" name="editor7" id="editor7" class="required textarea" title="Enter the Message"><?php echo $result[0]['BeTreatment'];?></textarea>


                            </td>
                        </tr>
                        </table>

                <?php



                }
                else 
                    {
                        $sel="SELECT $format as rff FROM `tbl_referreg` WHERE `ref_id`='".$id."'";
        $reffer=$objAdmin->ExecuteQuery($sel,'select');

?>
          <table><tr>
           <td><strong>Assessment Format:</strong></td>
            <td><textarea cols="80" style="margin-left:32px;"  rows="30" name="editor1" id="editor1" class=" textarea" title="Enter the Message"><?php echo $reffer[0]['rff']; ?></textarea></td></tr></table>

                 <?php }
                ?>

最佳答案

我尝试复制查看 HTML 和 JS,发现您正在替换 editor1、editor2、editor3 和 editor4,但您也有编辑器 5-7 的文本区域。这些是你所缺少的吗?如果是这样,只需在需要的地方添加 .replace(...) 即可。

我首先建议您稍微清理一下代码,以便您和其他人更容易了解发生了什么。这是我用来测试的示例:

<textarea cols="80" rows="10" name="editor2" id="editor2" class="required textarea" title="Enter the Message">A</textarea>
<textarea cols="80" rows="10" name="editor3" id="editor3" class="required textarea" title="Enter the Message">B</textarea>
<textarea cols="80" rows="10" name="editor4" id="editor4" class="required textarea" title="Enter the Message">C</textarea>
<textarea cols="80" rows="10" name="editor5" id="editor5" class="required textarea" title="Enter the Message">D</textarea>
<textarea cols="80" rows="10" name="editor6" id="editor6" class="required textarea" title="Enter the Message">E</textarea>
<textarea cols="80" rows="10" name="editor7" id="editor7" class="required textarea" title="Enter the Message">F</textarea>
<textarea cols="80" style="margin-left:32px;" rows="30" name="editor1" id="editor1" class=" textarea" title="Enter the Message">G</textarea>

<script type="text/javascript" src="http://cdn.jsdelivr.net/ckeditor/4.0/ckeditor.js"></script>
<script>
    function refer()
    {
        var config = {                  
            fontSize_sizes : "8px;9px;10px;12px;14px;16px;18px;20px;22px;24px;26px;28px;30px;32px;34px",
            toolbar :
            [
            ['Bold', 'Italic','Underline'],
            ['Source', '-', 'Undo','Redo'],
            ['Format','Font','FontSize'],
            ['TextColor','BGColor'],
            ['NumberedList','BulletedList','-','Blockquote'],
            ['Link', 'Unlink','SpecialChar'],
            ],
            // Strip CKEditor smileys to those commonly used in BBCode.
            smiley_images :
            [
            'regular_smile.gif','sad_smile.gif','wink_smile.gif','teeth_smile.gif','tounge_smile.gif',
            'embaressed_smile.gif','omg_smile.gif','whatchutalkingabout_smile.gif','angel_smile.gif','shades_smile.gif',
            'cry_smile.gif','kiss.gif'
            ],
            smiley_descriptions :
            [
            'smiley', 'sad', 'wink', 'laugh', 'cheeky', 'blush', 'surprise',
            'indecision', 'angel', 'cool', 'crying', 'kiss'
            ]
        };

        CKEDITOR.replace( 'editor1', config);
        CKEDITOR.replace( 'editor2', config);
        CKEDITOR.replace( 'editor4', config);
        CKEDITOR.replace( 'editor3', config);
        // Those got replaced nicely
    }
</script>

关于javascript - 动态页面中的CK编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14139273/

相关文章:

javascript - 使用 jQuery 查找文本并删除

javascript - 游戏网站需要修复故障

javascript - jQuery slider - 将最小值和最大值设置为与两个 handle 的值相同

javascript - CKEditor "insertHtml"适用于 FF 但不适用于 IE8

javascript - 在 rails 中为 CKEditor 添加自定义工具栏

jquery - ckeditor 火狐 NS_ERROR_FAILURE : ( should I ignored it ? )

widget - 如何为小部件按钮指定单独的工具栏组?

javascript - Bootstrap 和 AngularJS 之间的冲突

javascript - 使用 Jquery AJAX 检索 PHP post 请求数据

javascript - iframe 中元素的 CKEditor 内联编辑器