2014-05-10 4 views
1

다른 소스에서 아래 코드를 "빌 렸습니다." 내가 말할 수있는 한 기본적으로 this MathJax 데모 페이지와 동일합니다. 내가 가지고있는 문제는 홀수 번호 키 누름에 대해 입력 한 결과가 표시되지 않는다는 것입니다. 예를 들어 첫 번째 문자를 입력하면 MathPreview div에 아무 것도 표시되지 않습니다. 그러나 두 번째 문자를 입력 한 후 처음 두 문자를 봅니다. 이 패턴은 MathJax가 심지어 키 누름을 토글하는 것처럼 반복되지만, 홀수 키 누름의 경우에는 꺼집니다. 왜 이런 일이 일어나고 있는거야? 위에 링크 된 데모 페이지에서는이 문제가 발생하지 않습니다.LaTeX를 입력하면 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> 

답변

4

문제는 MathBuffer와 MathPreview가 중첩되어 있다는 것입니다. 그들은 형제가되어야합니다. 이 코드는 한 버퍼를 보여주고 다른 버퍼는 조판하고 두 버퍼를 전환하는 이중 버퍼링 기술을 사용합니다. 하나는 표시되고 다른 하나는 숨겨집니다. 하나가 다른 내부에 있으면 다른 모든 키 입력 만 결과를 볼 수 있습니다.

또한 버퍼의 내용이 입력으로 바뀌므로 MathPreview 버퍼를 바꾸면 MathBuffer와 그 안에 들어있는 스크립트가 제거됩니다. 링크 된 MathJax 페이지에서 두 div (MathPreview 및 MathBuffer)는 중첩되지 않으며 초기화 스크립트는 둘 다 (중첩되지 않음) 후에 발생합니다.

중첩 문제를 해결하면 문제가 해결 될 것이라고 생각합니다.

+0

그게 전부 였어! 감사! –

관련 문제