Javascript 如何在使用driver.js创建的弹出窗口上使用MathJax?
我使用生成弹出框来显示页面。虽然MathJax正在为基本元素工作,但我不知道如何在Popover上使用它。我遵循并尝试在生成popover时重新运行MathJax,但我无法使其工作 下面是一个描述问题的小示例:Javascript 如何在使用driver.js创建的弹出窗口上使用MathJax?,javascript,mathjax,Javascript,Mathjax,我使用生成弹出框来显示页面。虽然MathJax正在为基本元素工作,但我不知道如何在Popover上使用它。我遵循并尝试在生成popover时重新运行MathJax,但我无法使其工作 下面是一个描述问题的小示例: 当\(a\ne 0\)时,有两种解决方案\(ax^2+bx+c=0\),它们是\[x={-b\pm\sqrt{b^2-4ac}\over 2a}.\] 单击以显示弹出窗口 //定义popover 常量驱动程序=新驱动程序(); 驱动程序定义([ { 元素:'#btn', 流行音乐:
当\(a\ne 0\)时,有两种解决方案\(ax^2+bx+c=0\),它们是\[x={-b\pm\sqrt{b^2-4ac}\over 2a}.\]
单击以显示弹出窗口
//定义popover
常量驱动程序=新驱动程序();
驱动程序定义([
{
元素:'#btn',
流行音乐:{
标题:“测试MathJax”,
描述:“当\(a\ne 0\)时,有两种解决方案\(ax^2+bx+c=0\)是\[x={-b\pm\sqrt{b^2-4ac}\over 2a}.\]',
位置:'底部'
}
}
]);
设btn=document.querySelector('#btn');
//单击按钮时,应显示popover,并且应重新运行MathJax,以便正确显示popover中的数学
btn.addEventListener('click',function(){
driver.start();
Queue([“Typeset”,MathJax.Hub,“driver popover item”]);
});
这可能是由于driver.js
的构建方式造成的,但我自己没有足够的JavaScript知识来检查这一点,而且GitHub repo目前似乎相当不活跃
有人有想法吗?一些观察:
反斜杠(\)需要转义,以便在javascript中显示在字符串中。在HTML标记中不需要这样做
在最新版本中重新运行MathJax的命令是MathJax.typeset()。我推迟了,以便司机有机会让箱子出现
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Files for MathJax -->
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<!-- Files for driver.js -->
<script src="https://unpkg.com/driver.js/dist/driver.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/driver.js/dist/driver.min.css">
</head>
<body>
<p>
When \(a \ne 0\), there are two solutions to \(ax^2 + bx + c = 0\) and they are \[x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]
</p>
<button id="btn">Click to show popover</button>
<script>
// Define the popover
const driver = new Driver();
driver.defineSteps([
{
element: '#btn',
popover: {
title: 'Test MathJax',
description: 'When \\(a \\ne 0\\), there are two solutions to \\(ax^2 + bx + c = 0\\) and they are \\[x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}.\\]',
position: 'bottom'
}
}
]);
let btn = document.querySelector('#btn');
// When button is clicked, popover should appear and MathJax should re-run so that the math in the popover is correctly displayed
btn.addEventListener('click', function(){
driver.start();
// MathJax.Hub.Typeset(document.querySelector(".driver-popover-description"));
setTimeout(function() {
MathJax.typeset();
}, 1000);
});
</script>
</body>
</html>
当\(a\ne 0\)时,有两种解决方案\(ax^2+bx+c=0\),它们是\[x={-b\pm\sqrt{b^2-4ac}\over 2a}.\]
单击以显示弹出窗口
//定义popover
常量驱动程序=新驱动程序();
驱动程序定义([
{
元素:'#btn',
流行音乐:{
标题:“测试MathJax”,
描述:“当\\(a\\ne 0\\)时,有两种解决方案\\(ax^2+bx+c=0\\),它们是\\[x={-b\\pm\\sqrt{b^2-4ac}\\over 2a}.\\]',
位置:'底部'
}
}
]);
设btn=document.querySelector('#btn');
//单击按钮时,应显示popover,并且应重新运行MathJax,以便正确显示popover中的数学
btn.addEventListener('click',function(){
driver.start();
//Typeset(document.querySelector(“.driver popover description”);
setTimeout(函数(){
typeset();
}, 1000);
});
虽然Eduardo Poco的答案适用于单个popover,但它不适用于本次巡演中后面的popover。此外,在执行“Next”和“Prev”时(因此回到应用MathJax的第一个popover),MathJax不再应用
要将MathJax应用于所有driver.js
popover(我想其他教程库也有类似的功能),可以使用onHighlighted
对每个突出显示的popover应用MathJax
因此,我的帖子中的例子是:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Files for MathJax -->
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<!-- Files for driver.js -->
<script src="https://unpkg.com/driver.js/dist/driver.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/driver.js/dist/driver.min.css">
</head>
<body>
<p>
When \(a \ne 0\), there are two solutions to \(ax^2 + bx + c = 0\) and they are \[x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]
</p>
<button id="btn">Click to show popover</button>
<p id = "test2">Some text</p>
<script>
const driver = new Driver({
onHighlighted: () => {
setTimeout(function() {
MathJax.typeset();
}, 400);
}
});
driver.defineSteps([
{
element: '#btn',
popover: {
title: 'Test MathJax',
description: '\\(a \\ne 0\\), there are two solutions to \\(ax^2 + bx + c = 0\\) and they are \\[x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}.\\]',
position: 'bottom'
}
},
{
element: '#test2',
popover: {
title: 'Test MathJax',
description: '\\(a \\ne 0\\)',
position: 'bottom'
}
}
]);
let btn = document.querySelector('#btn');
btn.addEventListener('click', function(){
driver.start();
});
</script>
</body>
</html>
当\(a\ne 0\)时,有两种解决方案\(ax^2+bx+c=0\),它们是\[x={-b\pm\sqrt{b^2-4ac}\over 2a}.\]
单击以显示弹出窗口
一些文本
常量驱动程序=新驱动程序({
突出显示:()=>{
setTimeout(函数(){
typeset();
}, 400);
}
});
驱动程序定义([
{
元素:'#btn',
流行音乐:{
标题:“测试MathJax”,
描述:“\\(a\\ne 0\”,有两种解决方案\\(ax^2+bx+c=0\\),它们是\\[x={-b\\pm\\sqrt{b^2-4ac}\\over 2a}.\\]”,
位置:'底部'
}
},
{
元素:'#test2',
流行音乐:{
标题:“测试MathJax”,
描述:“\\(a\\ne 0\\)”,
位置:'底部'
}
}
]);
设btn=document.querySelector('#btn');
btn.addEventListener('click',function(){
driver.start();
});
注意:我将延迟设置为400毫秒,因为如果我输入一个较低的值,MathJax应用得太快(在弹出窗口的内容出现之前),因此不会修改内容。此值可能会为您更改(我尝试了多次查找)。您是否尝试了MathJax.Hub.Typeset(容器)来重新运行MathJax?我只是尝试替换
MathJax.Hub.Queue([“Typeset”,MathJax.Hub,“driver popover item]”)
byMathJax.Hub.Typeset(“驱动程序弹出项”)
但是没有successcontainer必须是对包含数学代码的元素的引用,而不是id字符串。对不起,我不明白,你能根据我帖子中的示例提出一个解决方案吗?我复制了你的代码,但它就是不起作用(MathJax未应用),对你有效吗?我从未使用过driver.js,但在动态数学内容方面也遇到了同样的问题。是否有指向此页面的公共链接,以便我可以使用devTools进行测试?不,我刚刚构建了此示例,但它不在公共页面上。我仍然是web开发的新手,您不能将devTools与代码一起使用吗