在具有以下代码的页面上,textarea 不会使用我键入的文本进行更新,直到我单击它为止。为什么?

<html>

<head>
  <style>
    #canvas {
      float: left;
    }
  </style>
  <script>
    window.onload = function() {
      var canvas = document.getElementById('canvas');
      var ctx = canvas.getContext('2d');
      ctx.translate(0, 0);
    }
  </script>
</head>

<body>
  <canvas id="canvas" width=439></canvas>
  <textarea></textarea>
</body>

</html>


我发现如果我执行以下任一操作,textarea 将正确更新:
  • 将 Canvas 的宽度更改为 438 或更少
  • 删除 ctx.translate(0,0);
  • 删除 float: left;

  • 这只发生在 Chrome 中,我无法在 Firefox 中重现。
    我正在运行 Chrome 39.0.2171.95 和 Firefox 34.0.5

    最佳答案

    这真是奇怪的行为。您可以通过向 textarea 添加相对定位来修复它:

    <html>
    
    <head>
      <style>
        #canvas {
          float: left;
        }
        textarea {
          position: relative;
        }
      </style>
      <script>
        window.onload = function() {
          var canvas = document.getElementById('canvas');
          var ctx = canvas.getContext('2d');
          ctx.translate(0, 0);
        }
      </script>
    </head>
    
    <body>
      <canvas id="canvas" width=439></canvas>
      <textarea></textarea>
    </body>
    
    </html>

    关于javascript - 在 Chrome 中输入时 HTML 文本区域不会更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27578307/

    10-12 00:02
    查看更多