JavaScript / CodeMirror - 刷新文本区域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5364909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:57:11  来源:igfitidea点击:

JavaScript / CodeMirror - refresh textarea

javascriptjquerycodemirror

提问by Alex

How do I use the refresh function from CodeMirror 2?

如何使用 CodeMirror 2 的刷新功能?

refresh()

刷新()

If your code does something to change the size of the editor element (window resizes are already listened for), or unhides it, you should probably follow up by calling this method to ensure CodeMirror is still looking as intended.

如果您的代码做了一些事情来改变编辑器元素的大小(窗口调整大小已经被监听),或者取消隐藏它,您可能应该通过调用此方法进行跟进,以确保 CodeMirror 仍然按预期显示。

I want to refresh all textareas after a link is clicked

单击链接后,我想刷新所有文本区域

I tried

我试过

  $('.CodeMirror').each(function(){
    getElementById($(this).attr('id')).refresh();
  });

but it doesn't work....

但它不起作用......

采纳答案by Marijn

The refresh method (just like all other CodeMirror methods) does not live on the DOM node, but on the instance object that's returned when you create the editor (by calling CodeMirror or CodeMirror.fromTextArea). So you'll have to store those somewhere for this to work.

refresh 方法(就像所有其他 CodeMirror 方法一样)并不存在于 DOM 节点上,而是存在于创建编辑器(通过调用 CodeMirror 或 CodeMirror.fromTextArea)时返回的实例对象上。所以你必须将它们存储在某个地方才能工作。

回答by jkschneider

When you instantiate the CodeMirror instance, it is placed as a property on the wrapper div.

当您实例化 CodeMirror 实例时,它会作为一个属性放置在包装器 div 上。

$('.CodeMirror').each(function(i, el){
    el.CodeMirror.refresh();
});

The above snippet does not recreate the editor, but instead uses the existing one.

上面的代码片段不会重新创建编辑器,而是使用现有的编辑器。