【发布时间】:2021-12-25 19:34:37
【问题描述】:
这是我在 stackflow 上的第一篇文章。我对编程很陌生,但 我正在尝试弄清楚如何有效地生成一个多功能函数,该函数将允许我一次为多个 div/块分别显示信息。 例如, 这个函数只对这个特定的按钮和 div 有效。
<!-- Function 1 -->
(function () {
var n = 1;
window.ShowStep1 = function () {
document.getElementById("Step1" + "-" + n++).style.visibility = "visible";
if (!document.getElementById("Step1" + "-" + n)) {
document.getElementById("step1").disabled = true;
}
document.getElementById("reset1").disabled = false;
}
window.ResetSteps1 = function () {
document.getElementById("step1").disabled = false;
document.getElementById("reset1").disabled = true;
var i = 1, step1; n = 1;
while (step1 = document.getElementById("Step1" + "-" + i)) {
step1.style.visibility = "hidden";
i++
}
}
})();
它仅适用于以下情况:
<p>
<input type="button" onclick="ShowStep1()" value="Steps" id="step1"/>
<input type="button" onclick="ResetSteps1()" value="Reset" id="reset1" disabled="true"/>
</p>
但我想对许多具有唯一 ID 的按钮执行此操作,而不必手动创建新功能。截至目前,为了让我创建多个功能,我必须手动创建几个Function 1 代码集。如果说我想做其中的 10 个,我怎样才能更有效地做到这一点?代码工作正常,但我不想重复自己。此代码来自以下链接: MathJax-demos-web-link
但我以自己的方式对其进行了修改。任何帮助和提示将不胜感激!提前致谢。 由于某种原因,sn-p 中存在错误,但如果您在 jsfiddle 上运行它,它可以正常工作。这是整个脚本。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width">
<title>MathJax v3 dynamic equations using CSS and javascript</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script>
MathJax = {
tex: {inlineMath: [['$', '$'], ['\\(', '\\)']]},
chtml: {
displayAlign: 'left'
},
startup: {
pageReady: function () {
//
// Do the usual startup (which does a typeset).
// When that is all done, un-hide the page.
//
return MathJax.startup.defaultPageReady().then(function () {
document.getElementById("hidden").disabled = true;
});
}
}
};
</script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
<script type="text/javascript">
<!-- Function 1 -->
(function () {
var n = 1;
window.ShowStep1 = function () {
document.getElementById("Step1" + "-" + n++).style.visibility = "visible";
if (!document.getElementById("Step1" + "-" + n)) {
document.getElementById("step1").disabled = true;
}
document.getElementById("reset1").disabled = false;
}
window.ResetSteps1 = function () {
document.getElementById("step1").disabled = false;
document.getElementById("reset1").disabled = true;
var i = 1, step1; n = 1;
while (step1 = document.getElementById("Step1" + "-" + i)) {
step1.style.visibility = "hidden";
i++
}
}
})();
<!-- Function 2 -->
(function () {
var n = 1;
window.ShowStep2 = function () {
document.getElementById("Step2" + "-" + n++).style.visibility = "visible";
if (!document.getElementById("Step2" + "-" + n)) {
document.getElementById("step2").disabled = true;
}
document.getElementById("reset2").disabled = false;
}
window.ResetSteps2 = function () {
document.getElementById("step2").disabled = false;
document.getElementById("reset2").disabled = true;
var i = 1, step2; n = 1;
while (step2 = document.getElementById("Step2" + "-" + i)) {
step2.style.visibility = "hidden";
i++
}
}
})();
</script>
<style>
#Step1-1, #Step1-2, #Step1-3, #Step1-4, #Step1-5,
#Step2-1, #Step2-2, #Step2-3, #Step2-4, #Step2-5 {
visibility: hidden;
}
</style>
</head>
<body>
<h1>Dynamic Equations in MathJax</h1>
<div id="frame">
<p>
Expand the following:
\begin{align}
(x+1)^2
&= \cssId{Step1-1}{(x+1)(x+1)} \\[3px]
&\cssId{Step1-2}{{} = x(x+1) + 1(x+1)} \\[3px]
&\cssId{Step1-3}{{} = (x^2+x) + (x+1)} \\[3px]
&\cssId{Step1-4}{{} = x^2 + (x + x) + 1} \\[3px]
&\cssId{Step1-5}{{} = x^2 + 2x + 1}
\end{align}
</p>
<p>
<input type="button" onclick="ShowStep1()" value="Steps" id="step1"/>
<input type="button" onclick="ResetSteps1()" value="Reset" id="reset1" disabled="true"/>
</p>
</div>
<div id="frame">
<p>
Expand the following:
\begin{align}
(x+1)^2
&= \cssId{Step2-1}{(x+1)(x+1)} \\[3px]
&\cssId{Step2-2}{{} = x(x+1) + 1(x+1)} \\[3px]
&\cssId{Step2-3}{{} = (x^2+x) + (x+1)} \\[3px]
&\cssId{Step2-4}{{} = x^2 + (x + x) + 1} \\[3px]
&\cssId{Step2-5}{{} = x^2 + 2x + 1}
\end{align}
</p>
<p>
<input type="button" onclick="ShowStep2()" value="Steps" id="step2"/>
<input type="button" onclick="ResetSteps2()" value="Reset" id="reset2" disabled="true"/>
</p>
</div>
</body>
</html>
【问题讨论】:
标签: javascript html css mathjax