如何在ckeditor中显示正确的数学公式格式

时间:2017-10-05 04:41:47

标签: javascript jquery html jquery-ui

我正面临着如何在ckeditor中显示匹配公式的正确格式的问题,我试图搜索很多方法,但似乎不能..

这是我的消息来源:

<html>
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script src="https://cdn.ckeditor.com/4.7.3/standard/ckeditor.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/x-mathjax-config">MathJax.Hub.Config({tex2jax: {inlineMath: [['\\(','\\)']]}});</script>     

<script type="text/javascript">
    $(function(){
    init();
    });
</script>

<script language="javascript">
    function init()
    {
        var strArea = "";            
        strArea += "<table border='0' cellpadding='0' cellspacing='0' align='left' valign='top' width='100%' id='contents2'>";

        strArea += "<tr><td>";
        strArea += "<table border='0' cellpadding='0' cellspacing='0' align='left' valign='top' width='100%'>";
        strArea += "<tr><td height='20'></td></tr><tr><td>"; 
        strArea += "<table border='3' cellpadding='0' cellspacing='0' align='left' valign='top'>";
        strArea += "<tr>";
        strArea += "<td width='20' valign='top' style='line-height:2.1'><div id='quizno'>\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)</div></td>";
        strArea += "<td width='5'></td>";
        strArea += "<td style='line-height:2.1'><H1><div id='quiz'>aaaa</div></H1></td>";
        strArea += "</tr>";
        strArea += "</table></td></tr>";
        strArea += "<tr><td height='7'>bbb</td></tr><tr><td>"; 
        strArea += "<table border='1' cellpadding='0' cellspacing='0' align='left' valign='top'>";
        strArea += "</table></td></tr></table></td></tr>";          
        strArea += "</table></td></tr></table>";
        document.all.quizdiv.innerHTML = strArea;
        // CKEDITOR.instances.ir4.getData()
        // CKEDITOR.instances.quizdiv.getData(document.getElementById('quizdiv').innerHTML) = strArea;          
    }       
    </script>    
    </head>
   <body>   
     <div style="overflow: auto; height: 700; width: 100%" id="centerdiv">
     <div id="quizdiv" style="width: 100%;"></div>                                      
 </div>
 </body>
 </html>

我尝试使用数学公式的默认文本显示:(x = {-b \ pm \ sqrt {b ^ 2-4ac} \ over 2a})但显示不正确

enter image description here

如何显示正确的数学公式格式?感谢..

1 个答案:

答案 0 :(得分:1)

我发现了不同的问题......

  1. CKEditor必须应用于textarea ...

    <textarea id="quizdiv" style="width: 100%;"></textarea>
    
  2. 你必须使用标准版的CKEditor而不是标准,因为标准不包括额外的插件......

    <script src="https://cdn.ckeditor.com/4.7.3/standard-all/ckeditor.js"></script>
    
  3. 您必须配置CKEDITOR.config.extraPluginsCKEDITOR.config.mathJaxLib ...

    CKEDITOR.config.extraPlugins = 'mathjax';
    CKEDITOR.config.mathJaxLib = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_HTML';
    
  4. 您必须将公式放在<span class='math-tex'> ...

    <span class="math-tex">\(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)</span>
    
  5. 您正在字符串中创建公式,因此您必须转义'\'字符以使它们出现在字符串中,以便插件可以读取它们...

    strArea += "<td width='50%' valign='top' style='line-height:2.1'><span class='math-tex'>\\(x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}\\)</span></td>";
    
  6. 这里有一个工作文件......

    https://fiddle.jshell.net/rigobauer/3qgeL5ae/

    我希望它有所帮助