Javascript 为什么MathJax在我键入LaTeX时会出现开关?
我从其他来源“借用”了下面的代码。据我所知,基本上和 MathJax演示页面。我遇到的问题是,我没有看到我为奇数按键输入的结果。例如,当我键入第一个字符时,我在Javascript 为什么MathJax在我键入LaTeX时会出现开关?,javascript,mathjax,Javascript,Mathjax,我从其他来源“借用”了下面的代码。据我所知,基本上和 MathJax演示页面。我遇到的问题是,我没有看到我为奇数按键输入的结果。例如,当我键入第一个字符时,我在MathPreviewdiv中看不到任何内容。但是,在键入第二个字符后,我会看到前两个字符。此模式会重复,就像MathJax为偶数按键打开,但为奇数按键关闭一样。知道为什么会这样吗?我在上面链接的演示页面上没有出现这种情况 <!DOCTYPE html> <html> <head> <ti
MathPreview
div中看不到任何内容。但是,在键入第二个字符后,我会看到前两个字符。此模式会重复,就像MathJax为偶数按键打开,但为奇数按键关闭一样。知道为什么会这样吗?我在上面链接的演示页面上没有出现这种情况
<!DOCTYPE html>
<html>
<head>
<title>Mathematics</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<link rel="stylesheet" href="assets/css/styles.css">
<script src="bower_components/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
var Preview = {
delay: 150, // delay after keystroke before updating
preview: null, // filled in by Init below
buffer: null, // filled in by Init below
timeout: null, // store setTimeout id
mjRunning: false, // true when MathJax is processing
oldText: null, // used to check if an update is needed
// Get the preview and buffer DIV's
Init: function () {
this.preview = document.getElementById("MathPreview");
this.buffer = document.getElementById("MathBuffer");
},
// Switch the buffer and preview, and display the right one.
// (We use visibility:hidden rather than display:none since
// the results of running MathJax are more accurate that way.)
SwapBuffers: function () {
var buffer = this.preview, preview = this.buffer;
this.buffer = buffer; this.preview = preview;
buffer.style.visibility = "hidden"; buffer.style.position = "absolute";
preview.style.position = ""; preview.style.visibility = "";
},
// This gets called when a key is pressed in the textarea.
// We check if there is already a pending update and clear it if so.
// Then set up an update to occur after a small delay (so if more keys
// are pressed, the update won't occur until after there has been
// a pause in the typing).
// The callback function is set up below, after the Preview object is set up.
Update: function () {
if (this.timeout) {clearTimeout(this.timeout)}
this.timeout = setTimeout(this.callback,this.delay);
},
// Creates the preview and runs MathJax on it.
// If MathJax is already trying to render the code, return
// If the text hasn't changed, return
// Otherwise, indicate that MathJax is running, and start the
// typesetting. After it is done, call PreviewDone.
CreatePreview: function () {
Preview.timeout = null;
if (this.mjRunning) return;
var text = document.getElementById("MathInput").value;
if (text === this.oldtext) return;
this.buffer.innerHTML = this.oldtext = text;
this.mjRunning = true;
MathJax.Hub.Queue(
["Typeset",MathJax.Hub,this.buffer],
["PreviewDone",this]
);
},
// Indicate that MathJax is no longer running,
// and swap the buffers to show the results.
PreviewDone: function () {
this.mjRunning = false;
this.SwapBuffers();
}
};
// Cache a callback to the CreatePreview action
Preview.callback = MathJax.Callback(["CreatePreview",Preview]);
Preview.callback.autoReset = true; // make sure it can run more than once
</script>
<script type="text/x-mathjax-config;executed=true">
MathJax.Hub.Config({
showProcessingMessages: false,
tex2jax: { inlineMath: [['$','$'],['\\(','\\)']] }
});
</script>
</head>
<body>
<div class="content">
<div id="MathJax_Message" style="display: none;"></div>
<div class="left_half_page">
<div class="content">
<div class="fill_with_padding">
<textarea class="content no_border" id="MathInput" onkeyup="Preview.Update()"></textarea>
</div>
</div>
</div>
<div class="right_half_page">
<div class="content">
<div class="fill_with_padding">
<div id="MathPreview" class="content">
<div id="MathBuffer">
<div>
<script>Preview.Init();</script>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
数学
变量预览={
延迟:150,//更新前击键后延迟
预览:null,//由下面的Init填写
缓冲区:null,//由下面的Init填充
超时:null,//存储设置超时id
mjRunning:false,//当MathJax正在处理时为true
oldText:null,//用于检查是否需要更新
//获取预览和缓冲区DIV
Init:函数(){
this.preview=document.getElementById(“MathPreview”);
this.buffer=document.getElementById(“MathBuffer”);
},
//切换缓冲区和预览,并显示右侧的缓冲区。
//(我们使用可见性:隐藏而不是显示:无)
//这样运行MathJax的结果更准确。)
SwapBuffers:函数(){
var buffer=this.preview,preview=this.buffer;
this.buffer=buffer;this.preview=preview;
buffer.style.visibility=“hidden”buffer.style.position=“absolute”;
preview.style.position=“”;preview.style.visibility=“”;
},
//当在文本区域中按下一个键时,将调用此函数。
//我们检查是否已经有一个挂起的更新,如果有,则清除它。
//然后设置一个更新在一个小的延迟后发生(因此如果有更多的键
//如果按下,则只有在出现
//打字时的停顿)。
//设置预览对象后,将在下面设置回调函数。
更新:函数(){
if(this.timeout){clearTimeout(this.timeout)}
this.timeout=setTimeout(this.callback,this.delay);
},
//创建预览并在其上运行MathJax。
//如果MathJax已经在尝试呈现代码,则返回
//如果文本未更改,请返回
//否则,指示MathJax正在运行,并启动
//排版。完成后,调用PreviewOne。
CreatePreview:函数(){
Preview.timeout=null;
如果(this.mjRunning)返回;
var text=document.getElementById(“MathInput”).value;
如果(text==this.oldtext)返回;
this.buffer.innerHTML=this.oldtext=text;
this.mjRunning=true;
MathJax.Hub.Queue(
[“Typeset”,MathJax.Hub,this.buffer],
[“PreviewOne”,本页]
);
},
//表示MathJax不再运行,
//并交换缓冲区以显示结果。
PreviewOne:函数(){
this.mjRunning=false;
这个。SwapBuffers();
}
};
//缓存对CreatePreview操作的回调
Preview.callback=MathJax.callback([“CreatePreview”,Preview]);
Preview.callback.autoReset=true;//确保它可以运行多次
MathJax.Hub.Config({
showProcessingMessages:false,
tex2jax:{inlineMath:[['$','$'],['\\(','\\')']}
});
Preview.Init();
问题在于您的MathBuffer和MathPreview是嵌套的。他们应该是兄弟姐妹。该代码使用双缓冲技术,显示一个缓冲区,而另一个正在排版,然后切换这两个缓冲区。一个显示,另一个隐藏。如果一个在另一个内,则只会每隔一次击键看到结果
另外,请注意,缓冲区的内容将被输入替换,因此当替换MathPreview缓冲区时,将删除MathBuffer及其包含的脚本。请注意,在链接到的MathJax页面中,两个div(MathPreview和MathBuffer)没有嵌套,初始化脚本出现在它们之后(没有嵌套在它们之中)
如果你解决了嵌套问题,我想它会对你有用。问题是你的MathBuffer和MathPreview是嵌套的。他们应该是兄弟姐妹。该代码使用双缓冲技术,显示一个缓冲区,而另一个正在排版,然后切换这两个缓冲区。一个显示,另一个隐藏。如果一个在另一个内,则只会每隔一次击键看到结果 另外,请注意,缓冲区的内容将被输入替换,因此当替换MathPreview缓冲区时,将删除MathBuffer及其包含的脚本。请注意,在链接到的MathJax页面中,两个div(MathPreview和MathBuffer)没有嵌套,初始化脚本出现在它们之后(没有嵌套在它们之中) 如果您修复了嵌套p