如何使用 Javascript 设置 CodeMirror 编辑器的值?

发布于 2024-12-19 17:29:11 字数 548 浏览 3 评论 0原文

我尝试在以下代码中设置 id4:

<div id="id1">
    <div id="id2">
        <div id="id3">
            <textarea id="id4"></textarea>
        </div>
    </div>
</div>

通过使用此代码:

document.getElementById('id4').value = "...";

和此:

document.getElementById('id3').getElementsByTagName('textarea')[0].value = "...";

但没有任何效果。

更新:

textarea 被 CodeMirror 编辑器取代。我如何为其设置值?

非常感谢您的帮助!

I try to set id4 in the following code:

<div id="id1">
    <div id="id2">
        <div id="id3">
            <textarea id="id4"></textarea>
        </div>
    </div>
</div>

By using this code:

document.getElementById('id4').value = "...";

And this:

document.getElementById('id3').getElementsByTagName('textarea')[0].value = "...";

But nothing works.

UPDATED:

The textarea is replaced by CodeMirror editor. How do I set value to it?

Thanks a lot for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

旧伤慢歌 2024-12-26 17:29:11

自 3.0 发布以来,执行此操作的方式略有变化。现在是这样的:

var textArea = document.getElementById('myScript');
var editor = CodeMirror.fromTextArea(textArea);
editor.getDoc().setValue('var msg = "Hi";');

The way to do this has changed slightly since the release of 3.0. It's now something like this:

var textArea = document.getElementById('myScript');
var editor = CodeMirror.fromTextArea(textArea);
editor.getDoc().setValue('var msg = "Hi";');
小伙你站住 2024-12-26 17:29:11

我喜欢例子。试试这个:

CodeMirror.fromTextArea(document.getElementById(id), {
        lineNumbers: true
    }).setValue("your code here");

I like examples. Try this:

CodeMirror.fromTextArea(document.getElementById(id), {
        lineNumbers: true
    }).setValue("your code here");
沦落红尘 2024-12-26 17:29:11

正如你所说,textarea 被 Codemirror 取代。但它被类为“CodeMirror”的元素所取代。您可以使用 querySelector 来获取元素。当前的 CodeMirror 实例(及其方法)附加到此元素。所以你可以这样做:

document.querySelector('.CodeMirror').CodeMirror.setValue('VALUE')

As you said, the textarea is replaced by Codemirror. But it is replaced by an element with the class "CodeMirror". You can use querySelector to get the element. The current CodeMirror instance (and its methods) is attached to this element. So you can do:

document.querySelector('.CodeMirror').CodeMirror.setValue('VALUE')
浮生面具三千个 2024-12-26 17:29:11

CodeMirror ~4.6.0 你可以这样做,假设你有一个 codemirror 对象:

var currentValue = myCodeMirrorObject.cm.getValue();
var str = 'some new value';
myCodeMirrorObject.cm.setValue(str);

CodeMirror ~4.6.0 you can do this, assuming you have a codemirror object:

var currentValue = myCodeMirrorObject.cm.getValue();
var str = 'some new value';
myCodeMirrorObject.cm.setValue(str);
ま昔日黯然 2024-12-26 17:29:11

您拥有的代码应该可以工作。失败的最可能的解释是元素在运行时不存在。如果是这样,解决方案是:

  • 移动 JS,使其出现在创建元素之后(例如,到 之前)
  • 延迟执行 JS,直到创建元素(例如,将其移动到您指定为 onload 事件处理程序的函数)

The code you have should work. The most likely explanation for it failing is that the elements do not exist at the time you run it. If so the solutions are to either:

  • Move the JS so it appears after the elements have been created (e.g. to just before </body>)
  • Delay execution of the JS until the elements have been created (e.g. by moving it to a function that you assign as the onload event handler)
ぽ尐不点ル 2024-12-26 17:29:11

这对我的(相当旧的)版本的 CodeMirror 有效:

var editor=CodeMirror.fromTextArea('textarea_id');

editor.setCode('your string');

This has worked for my (pretty old) version of CodeMirror:

var editor=CodeMirror.fromTextArea('textarea_id');

editor.setCode('your string');
清眉祭 2024-12-26 17:29:11

editor.setValue("YOUR_VALUE");

editor.setValue("YOUR_VALUE");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文