最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - CodeMirror markText is not working - Stack Overflow

programmeradmin6浏览0评论

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.

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.

Share Improve this question edited Mar 17, 2016 at 0:02 CMedina 4,2524 gold badges27 silver badges40 bronze badges asked Mar 16, 2016 at 22:27 GansGans 1293 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

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

See the documentation for more details: https://codemirror/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./ajax/libs/codemirror/5.12.0/codemirror.css"
<script type="text/javascript" src="https://cdnjs.cloudflare./ajax/libs/codemirror/5.12.0/codemirror.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare./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>

发布评论

评论列表(0)

  1. 暂无评论