如何使用MathQuill将多个ID显示为LaTeX方程式?

huangapple go评论61阅读模式
英文:

How can I show multiple id as latex equation by using mathquill?

问题

我正在尝试创建一个符号调色板,其中将有一个输入字段,我将单击包含Latex符号的按钮,该符号将成为字段中的输入。由于会有多个符号,我尝试使用多个按钮和MQ.StaticMath来显示Latex方程。但是只有第一个id接受我的Latex输出。不幸的是,其他id没有接受。我尝试过使用类而不是id,并在脚本中使用getElementByClassName,但也没有起作用。在这里,我分享了我的代码和截图。希望有人可以帮助我解决这个问题。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Run Mathquill</title>

    <!-- YOU NEED  mathquill.css , jquery and mathquill.js SEARCH AND PICK IT SOMEWHERE, SEARCH ON THE WEB IF THIS DOWN  NOT RUN-->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mathquill/0.10.1/mathquill.css"
        integrity="sha512-vPg9GqsZZ4LHv9BkFfZSXt7y4D7YaARPU2JFmpZug4EgtJJrumytMAFZkNSk2LSyqWir0TNbh2tBq7UJIMxvlA=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mathquill/0.10.1/mathquill.js"
        integrity="sha512-7jEhcM7FbjGHo1ejs1iw1J8FxcnACx7Z3lG29gQ5vTBe2U/gaQpzwjzPCyg32zTwXCloQDdorpLufmu0nBIqnQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer"></script>

    <!-- INFORM  THE LATEST AVERSION INTERFACE WITH THIS CODE   -->
    <script>
        var MQ = MathQuill.getInterface(2);
    </script>

</head>
<body>
<br>
<button class="symbol">\sqrt{}</button>
<button class="symbol">\frac{1}{2}</button>
<script>
    var symbols = document.getElementsByClassName('symbol');
    for (var i = 0; i < symbols.length; i++) {
        MQ.StaticMath(symbols[i]);
    }
</script>
</body>
</html>

输出:如果我注释掉一行,另一个工作得很好,但我需要同时或更多工作。

如何使用MathQuill将多个ID显示为LaTeX方程式?

如何使用MathQuill将多个ID显示为LaTeX方程式?

如图所示,只有一个正在工作。

英文:

I am trying to make a symbol palette where there will be an input field and I will click on a button that contains a latex symbol that will be the input in the field. As there will be multiple symbols, I am trying to use multiple buttons and MQ.StaticMath to show the latex equation. But the first id is accepting my latex output. Unfortunately, not the rest ids. I tried with class instead of id and using getElementByClassName in the script but not working as well. Here I am sharing my code and screenshots.
Hoping someone can help me out with it.

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
    &lt;title&gt;Run Mathquill&lt;/title&gt;


    &lt;!-- YOU NEED  mathquill.css , jquery and mathquill.js SEARCH AND PICK IT SOMEWHERE, SEARCH ON THE WEB IF THIS DOWN  NOT RUN--&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/mathquill/0.10.1/mathquill.css&quot;
        integrity=&quot;sha512-vPg9GqsZZ4LHv9BkFfZSXt7y4D7YaARPU2JFmpZug4EgtJJrumytMAFZkNSk2LSyqWir0TNbh2tBq7UJIMxvlA==&quot;
        crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot; /&gt;
    &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/mathquill/0.10.1/mathquill.js&quot;
        integrity=&quot;sha512-7jEhcM7FbjGHo1ejs1iw1J8FxcnACx7Z3lG29gQ5vTBe2U/gaQpzwjzPCyg32zTwXCloQDdorpLufmu0nBIqnQ==&quot;
        crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;

    &lt;!-- INFORM  THE LATEST AVERSION INTERFACE WITH THIS CODE   --&gt;
    &lt;script&gt;
        var MQ = MathQuill.getInterface(2);
    &lt;/script&gt;


&lt;/head&gt;
&lt;body&gt;
&lt;br&gt;
&lt;button id=&quot;symbol&quot;&gt;\sqrt{}&lt;/button&gt;
&lt;button id=&quot;symbol&quot;&gt;\frac{1}{2}&lt;/button&gt;
&lt;script&gt;
    var symbols = document.getElementById(&#39;symbol&#39;);
    MQ.StaticMath(symbols);
&lt;/script&gt;


&lt;/body&gt;

&lt;/html&gt;

And the output if I comment on one line another works perfectly but I need both or more to work.

如何使用MathQuill将多个ID显示为LaTeX方程式?

如何使用MathQuill将多个ID显示为LaTeX方程式?

As it's visible only one is working.

答案1

得分: 0

你有多个按钮元素需要处理,因此需要使用 JavaScript 的 forEach 方法。

<script>
   var MQ = MathQuill.getInterface(2);
   var symbols = document.querySelectorAll('#symbol');
   symbols.forEach(MQ.StaticMath);
</script>

如何使用MathQuill将多个ID显示为LaTeX方程式?



<details>
<summary>英文:</summary>

You have multiple button elements to handle. So you need to use js forEach.

~~~
&lt;script&gt;
   var MQ = MathQuill.getInterface(2);
   var symbols = document.querySelectorAll(&#39;#symbol&#39;);
   symbols.forEach(MQ.StaticMath);
&lt;/script&gt;
~~~

&lt;hr&gt;

[![Mathquill in buttons][1]][1]

&lt;hr&gt;


  [1]: https://i.stack.imgur.com/2e0k2.png

</details>



huangapple
  • 本文由 发表于 2023年1月9日 01:24:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定