为什么在加载此页面时未显示value属性“某些代码”?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="lib/codemirror-5.47.0/lib/codemirror.css">
<script src="lib/codemirror-5.47.0/lib/codemirror.js"></script>
<script src="lib/codemirror-5.47.0/mode/javascript/javascript.js"></script>
<script src="lib/codemirror-5.47.0/mode/powershell//powershell.js"></script>
<title>Code Mirror</title>
</head>
<body>
<h1>Hello Code Mirror</h1>
Code Mirror <a href="https://codemirror.net/doc/manual.html">Documenation</a>
</br>
<textarea id='jsTxtArea'></textarea>
</body>
<script>
var myCodeMirror = CodeMirror.fromTextArea(document.getElementById('jsTxtArea'), {
value: "some code",
lineNumbers:true,
mode:"javascript"
});
</script>
</html>
答案 0 :(得分:1)
您需要从文本区域获取值,如下所示:
var textarea = document.getElementById('jsTxtArea');
var myCodeMirror = CodeMirror.fromTextArea(textarea , {
value: textarea.innerHtml,
lineNumbers: true,
mode: 'javascript'
});
或者,如果您想在JavaScript中设置值:
var textarea = document.getElementById('jsTxtArea');
var myCodeMirror = CodeMirror.fromTextArea(textarea , {
lineNumbers: true,
mode: 'javascript'
});
// set the value
myCodeMirror.setValue('Some Code');