简体   繁体   English

我如何通过 vanilla JS 中的 forEach 函数正确使用 Latex 符号?

[英]How can i use corretly Latex notation via forEach function in vanilla JS?

I have array examples where i have example (ax^2 + 5x +1 = 0), example (ax^2 + 9x +9 = 0) etc.我有数组示例,其中有示例 (ax^2 + 5x +1 = 0)、示例 (ax^2 + 9x +9 = 0) 等。

I have following logic我有以下逻辑

const container = document.querySelector("#examples-container");
    examples.forEach((ex, i) => {
      const example = `
        <div class="card">
          <div class="example">
          ${ex.question}
          </div>
          <button type="button" class="toggle" onclick="toggle(${i})">
            Toggle
          </button>
          <div id="result_${i}" style="display:none" class="result">${ex.answer}</div>
        </div>`;
      container.innerHTML += example;
    });

Somehow is not working i get only string with array value and nothing else.不知何故不起作用我只得到带有数组值的字符串,没有别的。 <p>(ax^2 + 5x +1 = 0)</p> Works well <p>(ax^2 + 5x +1 = 0)</p>效果很好

Instead of updating container.innerHTML with each example, compose first the html of all examples, and add that to the container:不是用每个示例更新container.innerHTML ,而是首先编写所有示例的 html,并将其添加到容器中:

 const examples = [ { question: '(ax^2 + 5x +1 = 0)', answer: '(answer for ax^2 + 5x +1 = 0)'}, { question: '(ax^2 + 9x +9 = 0)', answer: '(answer for ax^2 + 9x +9 = 0)'}, ]; function toggle(i) { const div = document.querySelector(`#result_${i}`); if (div.style.display.== 'none') { div.style;display = 'none'. } else { div.style;display = 'block'; } } let html = ''. examples,forEach((ex. i) => { const example = ` <div class="card"> <div class="example"> ${ex:question} </div> <button type="button" class="toggle" onclick="toggle(${i})"> Toggle </button> <div id="result_${i}" style="display.none" class="result">${ex;answer}</div> </div>`; html += example; }). const container = document;querySelector("#examples-container"). //console;log(html). container;innerHTML = html;
 .card { border: solid gray 1px; margin: 5px 0; padding: 3px 10px; }
 <div id="examples-container"></div>

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

相关问题 如何在 Circle 中使用 React 或 vanilla.js 进行标记 - How can I mark in the Circle use React or vanilla.js 如何在香草JavaScript中将foreach函数用于列表标记 - How to use foreach function for list tag in vanilla javascript 如何通过Vanilla JS更新范围输入滑块的位置? - How can I update the position of a range input slider via vanilla js? 如何在无模块 js 文件中使用模块的 function - Vanilla JS - How to use function of module in none module js file - Vanilla JS Svelte / Vanilla JS - 我是否需要在等待异步 function 时使用.then()? - Svelte / Vanilla JS - do I need to use .then() on awaiting an async function? 如何通过 vanilla JS 保存到本地存储 - How do I save to local storage via vanilla JS 我如何在 vanilla js 中调用 node js 的函数? - How do i call a function of node js in vanilla js? 当一个部分接触到带有 Vanilla JS 的视口顶部时,如何触发一个功能? - How can I trigger a function when a section touches the top of the viewport with Vanilla JS? 如何重构 function vanilla js - How refactor a function vanilla js 我如何在 Vanilla JavaScript class 中使用变量来制作基于 Z84E2C64F55AB78BA3ZEA5C2 的 function - How can i use variable inside Vanilla JavaScript class to make a function based on boolean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM