Mathjax插入div写入文本ContentEditable div?

时间:2016-08-04 02:28:46

标签: javascript html mathjax

我参考了WYSIWYG Editor for Mathematical Equations using MathJax此代码

此代码在这里

<!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"> =&gt; <div contenteditable="true">代码。

我改变了一个div代码。但脚本错误。 “wh_test.html:27 Uncaught TypeError:selectedContent.setSelectionRange不是函数”

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

1 个答案:

答案 0 :(得分:0)

首先 contenteditable div中使用相同的 ID 。因此 it('should render an anchor tag', () => { sinon.spy(browserHistory, 'goBack'); const wrapper = mount(<GenericNotFound />; const onClick = wrapper.find('a').props().onClick; onClick(); expect(browserHistory.goBack).to.have.been.called; browserHistory.goBack.restore(); }); 需要更改为<textarea id="wrapSelectedContent"></textarea>代码。

其次 div没有值属性,您必须使用 innerHTML 来获得与textarea相似的结果。 因此如下 <div id="wrapSelectedContent" contenteditable="true"></div> 必须更改为var selectedContentValue = selectedContent.value.substring(start, end);