繁体   English   中英

如何强制更新HTML的TextArea元素?

[英]How to force update HTML's TextArea element?

对不起,对于新手问题,但这与WebKit问题有关。 我有下一个JS代码:

var Module = {
        preRun: [],
        postRun: [],
        print: (function() {
          var element = document.getElementById('output');
          if (element) element.value = ''; // clear browser cache
          return function(text) {
            text = Array.prototype.slice.call(arguments).join(' ');
            // These replacements are necessary if you render to raw HTML
            //text = text.replace(/&/g, "&");
            //text = text.replace(/</g, "&lt;");
            //text = text.replace(/>/g, "&gt;");
            //text = text.replace('\n', '<br>', 'g');
            console.log(text);
            if (element) {
              element.value += text + "\n";
              console.log('updated element.value');
              element.scrollTop = element.scrollHeight; // focus on bottom
            }
          };
        })

元素是textarea元素:

<textarea id="output" rows="8"></textarea>

我有使用该函数的printf代码,然后显示提示键入用户名。 因此,我希望在显示提示对话框之前,在浏览器控制台中看到“ updated element.value”并在textarea打印文本。

更改textareaelement.value += text + "\\n" )后,如何强制刷新?

它在chrome / firefox中按预期工作,但在Safari(WebKit)中失败-在显示提示对话框时,我看不到输出。

对我来说,这听起来似乎是无法完成的事情,但是可能有一些我不了解的特定于Webkit的黑客。 也许所有浏览器都在您期望的同一个渲染周期中进行两种更新,但是Safari首先显示提示,并以某种方式暂停其余更新,直到提示被消除为止。

最明显的解决方法是

print('lorem ipsum');
setTimeout(function(){
    var response = prompt('my question here');
    //handle the response
}, 10);

这很可能保证在提示锁定内容之前发生textarea更新。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM