简体   繁体   English

为什么MathJax在我的JavaScript代码输出中似乎无法正常工作?

[英]Why does MathJax not seem to be working properly in the output of my JavaScript code?

I'm trying to write a simple program that generates dot product questions. 我正在尝试编写一个生成点积问题的简单程序。 The code I have used is the following: 我使用的代码如下:

<!DOCTYPE html>
<html>
<body>

<head>
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript"
    src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
</script>
</head>



<button onclick="myFunction()">Generate Question</button>

<br>
<br>
<p id="demo2"></p>
<hr>
<p id="demo"></p>

<script>
function myFunction() {
var a = Math.floor((Math.random() * 10) + 1);
var b = Math.floor((Math.random() * 10) + 1);
var c = Math.floor((Math.random() * 10) + 1);
var x = Math.floor((Math.random() * 10) + 1);
var y = Math.floor((Math.random() * 10) + 1);
var z = Math.floor((Math.random() * 10) + 1);

var dot = (a*x)+(b*y)+(c*z);





document.getElementById("demo2").innerHTML = "Find the       solution to the dot product (" + a + ", " + b + ", " + c + ")" + "$\.   \cdot$" + "(" + x + ", " + y + ", " + z + ")";

document.getElementById("demo").innerHTML = "Solution: (" + a + ", " + b + ", " + c + ")" + "$\\cdot$" + "(" + x + ", " + y + ", " + z + ") = " + dot;

MathJax.Hub.Queue(['Typeset', MathJax.Hub, document.getElementById("demo")]);

}
</script>

</body>
</html>

However, the output this gives looks like 但是,这给出的输出看起来像

输出

Why does it render in this way? 为什么以这种方式渲染? It works properly in the solution, but not in the question. 它在解决方案中正常工作,但在问题中不起作用。

Aside from the missing double backslash pointed out by Peter and MvG in comments above, the reason the first math isn't typeset is that you have only asked MathJax to typeset the "demo" element, and the other math is in the "demo2" element. 除了上述注释中由Peter和MvG指出的缺失的双反斜线外,第一个数学未进行排版的原因是,您仅要求MathJax对"demo"元素进行排版,而另一个数学在"demo2"元件。 Try using 尝试使用

MathJax.Hub.Queue(['Typeset', MathJax.Hub, ["demo2","demo"]]);

instead. 代替。

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

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