简体   繁体   中英

Set CodeMirror style and addons with a html attribute

I have this CodeMirror ( http://codemirror.net/index.html ) text area:

<form><textarea id="code" name="code" codemirror-type='lineNumbers: false, styleActiveLine: true, matchBrackets: true;'>CODE HERE</textarea></form>

And as you can see I added the attribute "codemirror-type", I would like to set this style when the script is executed, I tried:

<script>
  var attrs = document.getElementById("code").getAttribute("codemirror-type");
  var editor = CodeMirror.fromTextArea(document.getElementById("code"), attrs);
</script>

But it does not work I suppose because it is not in the correct format ( http://codemirror.net/doc/manual.html#fromTextArea ).

I really need to style the code editor from an HTML attribute (or class or addition tag or whatever) How can I achieve this?

Solved, it was a JSON Parse issue, given a CodeMirror textarea like this:

<textarea id="code" name="code" codemirror-settings='{"lineNumbers": true, "styleActiveLine": true, "matchBrackets": true}'>CODE_HERE</textarea>

It can be rendered correctly like this:

var attrs = document.getElementById("code").getAttribute("codemirror-settings");
var obj = JSON.parse(attrs);
var editor = CodeMirror.fromTextArea(document.getElementById("code"), obj);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM