Mathjax 插入 div 写入文本 ContentEditable div?

Mathjax insert div write text ContentEditable div?

我参考了这个代码

代码在这里

<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
    <title>mathjx</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" contenteditable="true" style="width:300px; height:300px; border:2px solid #000;"></div>
<!-- Static Content displayed Successfully -->
<button onclick="wrapContent();">ConVert</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 = "$$" + selectedContentValue + "$$";
        pasteselectedContent.textContent = selectedContent.value.substring(0,start) + selectedContent.value.substring(end,textlength); 
        MathJax.Hub.Queue(["Typeset", MathJax.Hub, "pasteselectedContent"]);

    }
</script>   
</body>
</html>

代码运行良好。

现在,我要改变 <textarea id="wrapSelectedContent"> => <div contenteditable="true"> 标签。

我更改了一个 div 代码。但脚本错误。 "wh_test.html:27 Uncaught TypeError: selectedContent.setSelectionRange is not a function"

如何更改此代码。感谢阅读。

首先contenteditable[=25]中使用相同的ID =] div。所以 <textarea id="wrapSelectedContent"></textarea> 需要更改为 <div id="wrapSelectedContent" contenteditable="true"></div> 标记。

其次 div没有价值属性你必须使用innerHTML 以获得与 textarea 类似的结果。 因此遵循 var selectedContentValue = selectedContent.value.substring(start, end); 必须改成var selectedContentValue = selectedContent.innerHTML.substring(start, end);