CodeMirror markText无法正常工作 [英] CodeMirror markText is not working

查看:112
本文介绍了CodeMirror markText无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像这样使用CodeMirror来显示对User的一些XML响应.

HTML代码

 < body>< textarea id ="cm">#{bean.xmlResponse}</textarea></body> 

JS代码

  window.onload = function(){var editor = CodeMirror.fromTextArea(document.getElementById('cm'),{模式:"xml",主题:默认"});editor.getDoc().markText({line:5,ch:2},{line:5,ch:9},"color:red");}; 

现在,当我尝试通过使用无法正常工作的markText突出显示某些特定行时.当然,"xml"模式有效,但是第5行没有用红色突出显示.

非常感谢您的帮助.努力已经完成了3天.谢谢.

解决方案

您需要将options参数指定为地图,而不是字符串: {css:"color:red"}

有关更多详细信息,请参见文档: https://codemirror.net/doc/manual.html#markText

这是根据您的示例显示的代码片段,它按照您的描述进行工作(您可以忽略运行该代码片段所需的不必要的CSS/JS设置和示例xml):

  var editor = CodeMirror.fromTextArea(document.getElementById('cm'),{模式:"xml",主题:默认"});editor.getDoc().markText({行:5ch:10},{行:5ch:39},{CSS:颜色:红色"});  

  @import"https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.12.0/codemirror.css" 

 < script type ="text/javascript" src ="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.12.0/codemirror.js</script>< script type ="text/javascript" src ="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.12.0/mode/xml/xml.js"></script>< textarea id ="cm">< note>< to>/toto>< from> Jani</from><标题>提醒</标题>< body>这个周末别忘了我!</body></note></textarea>  

I am using CodeMirror like this to show some XML response to User.

HTML CODE

<body>
    <textarea id="cm" >#{bean.xmlResponse}</textarea>
</body>

JS CODE

window.onload = function () {              
    var editor = CodeMirror.fromTextArea(document.getElementById('cm'), {
                    mode: "xml",
                    theme: "default"
                });

    editor.getDoc().markText({line:5,ch:2},{line:5,ch:9},"color : red");
};

Now when I am trying to highlight some particular line by using markText which is not working. Of course "xml" mode is working but line 5 is not highlighted with color red.

I really appreciate your help. It's been 3 days am trying to get it done. Thanks.

解决方案

You need to specify the options parameter as a map, not a string: {css: "color : red"}

See the documentation for more details: https://codemirror.net/doc/manual.html#markText

Here's a snippet based on your example that is showing it working as you describe (you can ignore the unnecessary CSS/JS setup and example xml that were require to have the snippet running):

var editor = CodeMirror.fromTextArea(document.getElementById('cm'), {
  mode: "xml",
  theme: "default"
});

editor.getDoc().markText({
  line: 5,
  ch: 10
}, {
  line: 5,
  ch: 39
}, {
  css: "color : red"
});

@import "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.12.0/codemirror.css"

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.12.0/codemirror.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.12.0/mode/xml/xml.js"></script>
<textarea id="cm">
  <note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>

    <body>Don't forget me this weekend!</body>
  </note>
</textarea>

这篇关于CodeMirror markText无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆