问题
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.
回答1:
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>
来源:https://stackoverflow.com/questions/36048167/codemirror-marktext-is-not-working