I am using codemirror 2 and its working fine except that the editor's set value doesn't load into the editor until I click the editor and it becomes focused.

我希望编辑器显示自己的内容,而不必点击.有什么 idea 吗?

All of the codemirror demos work as expected so I figured maybe the textarea isn't focused so I tried that too.

$("#editor").focus();
var editor =    CodeMirror.fromTextArea(document.getElementById("editor"), {
                    mode: "text/html",
                    height: "197px",
                    lineNumbers: true
                });

推荐答案

必须在setValue()之后调用refresh().但是,必须使用setTimeout将刷新()延迟到CodeMirror/Browser根据新内容更新布局之后:

codeMirrorRef.setValue(content);
setTimeout(function() {
    codeMirrorRef.refresh();
},1);

It works well for me. I found the answer in here.

Jquery相关问答推荐

JQuery DateTimePicker-在 Select 中抓取星期几

使用动态类名的gm_addStyle?

Javascript 仅打印 iframe 内容

如何用 javascript/jquery 替换 url 参数?

如何使用 jQuery 的 getJSON() 方法传递请求标头?

在 select2 中使用 AJAX 进行标记

我如何从 ACE 编辑器中获得价值?

清除并刷新 jQuery Chosen 下拉列表

如何在 jQuery 中存储全局值(不一定是全局变量)?

调整浏览器大小时调整jqGrid的大小?

jQuery UI 滑块(以编程方式设置)

未捕获的语法错误:无法在文档上执行querySelector

谷歌图表重绘/zoom 窗口调整大小

datatable jquery - 表头宽度与正文宽度不对齐

jQuery DataTables:延迟搜索直到输入 3 个字符或单击按钮

带有 jQ​​uery 的饼图

jQuery ajax 成功回调函数定义

如何在纯 JavaScript 中模拟鼠标悬停以激活 CSS:悬停?

上传文件前验证文件扩展名

jQuery向左滑动并显示