gpt4 book ai didi

javascript - 使用 MathJax 的所见即所得数学方程编辑器

转载 作者:行者123 更新时间:2023-11-28 07:37:17 30 4
gpt4 key购买 nike

我正在尝试构建所见即所得编辑器来使用 MathJax 编写数学公式。我正在尝试从文本区域中选择随机内容并将其粘贴到 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 中显示的文本请帮助我构建这个所见即所得编辑器

最佳答案

问题已解决

只需插入

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

第 32 行之后

pasteselectedContent.textContent = selectedContent.value.substring(0,start) + " " + replace + " " + selectedContent.value.substring(end,textlength);

关于javascript - 使用 MathJax 的所见即所得数学方程编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28452359/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com