javascript – 使用MathJax的数学方程的WYSIWYG编辑器

我正在尝试使用MathJax构建WYSIWYG编辑器来编写数学公式.我试图从textarea中选择随机内容并将其粘贴到div中以将其转换为数学方程式.

<!DOCTYPE html>
<html>
<head>
    <title>Wrap Selected Content with Dummy Editor</title>
    <script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>;
    <script type="text/x-mathjax-config">
        MathJax.Hub.Config({tex2jax: { inlineMath: [ ['$','$'], ['\\(','\\)'] ] } });
        MathJax.Hub.Config({tex2jax: { displayMath: [ ['$$','$$'], ['\\(','\\)'] ] } });
    </script>
</head>
<body>
<!--Select some random text from this textarea and click button -->
<textarea id="wrapSelectedContent"></textarea>
<!-- Content should be load here using JavaScript and Convert it into Mathematical Function -->
<div id="pasteSelectedContent" style="width:300px; height:300px; border:2px solid #000;"></div>
<!-- Static Content displayed Successfully -->
<p>$$\sum(a+b)(c+d)$$</p>
<button onclick="wrapContent();">Put Summation Sign</button>
<script type="text/javascript">
    function wrapContent(){
        var selectedContent = document.getElementById("wrapSelectedContent");
        var pasteselectedContent = document.getElementById("pasteSelectedContent");
        var textlength = selectedContent.textLength;
        var start = selectedContent.selectionStart;
        var end = selectedContent.selectionEnd;
        selectedContent.focus();
        selectedContent.setSelectionRange(start,end); 
        var selectedContentValue = selectedContent.value.substring(start, end);
        var replace = "$$\\sum" + selectedContentValue + "$$";
        pasteselectedContent.textContent = selectedContent.value.substring(0,start) + " " + replace + " " + selectedContent.value.substring(end,textlength);        
        console.log("Start Index : " + start);
        console.log("End Index : " + end);
        console.log("Text Content Length : " + textlength);
        console.log(selectedContentValue);
    }
</script>
</body>
</html>

那么如何将div#pasteSelectedContent中显示的文本转换为数学方程式
请帮我构建这个WYSIWYG编辑器

解决方法:

问题解决了

只是插入

    MathJax.Hub.Queue(["Typeset", MathJax.Hub, "pasteselectedContent"]);

在第32行之后

pasteselectedContent.textContent = selectedContent.value.substring(0,start) + " " + replace + " " + selectedContent.value.substring(end,textlength);
上一篇:javascript-Mathjax仅在数据表的第一页上呈现


下一篇:javascript – 在fabric.js中写下乳胶公式