繁体   English   中英

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未捕获的TypeError:selectedContent.setSelectionRange不是函数”

它是如何工作的,请更改此代码。

首先contenteditable div中使用相同的ID 因此,需要将<textarea id="wrapSelectedContent"></textarea>更改为<div id="wrapSelectedContent" contenteditable="true"></div>标记。

其次, div没有value属性,您必须使用innerHTML获得与textarea类似的结果。 因此下面的var selectedContentValue = selectedContent.value.substring(start, end); 必须更改为var selectedContentValue = selectedContent.innerHTML.substring(start, end);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM