2

我正在尝试使用 CodeMirror 在文本区域中突出显示语法。我还在为 textarea 使用 Bootstrap 2.3。我得到了 Bootstrap 显示的 textarea 中的代码,但是在 CodeMirror 中没有突出显示语法,而是在 Chrome 控制台中得到了这个错误:

未捕获的类型错误:无法设置未定义的属性“显示”。

这是我用于给定表单的 Javascript,对象为空。

var myCodeMirror = CodeMirror.fromTextArea($('#formId'), {
    mode: {name: "python",
           version: 2,
           singleLineStringErrors: false},
    lineNumbers: true,
    //indentUnit: 4,
    smartIndent: true,
    tabSize: 2,
    indentWithTabs: true,
    tabMode: "shift",
    autofocus: true,
    matchBrackets: true
  });
4

1 回答 1

1

获取完整的代码集以查找错误会非常有帮助。见:http ://sscce.org/

我唯一能做的就是提供一段代码,让 bootstrap 和 codemirror 真正一起工作:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/2.3.2/js/bootstrap.min.js"></script>
  <link rel=stylesheet href="//codemirror.net/doc/docs.css">
  <link rel=stylesheet href="//codemirror.net/lib/codemirror.css">
  <link rel=stylesheet href="//codemirror.net/theme/night.css">
  <script src="//codemirror.net/lib/codemirror.js"></script>
  <script src="//codemirror.net/mode/python/python.js"></script>
</head>
<body>
<div class="container">
  <h2>Form control: textarea</h2>
  <p>The form below contains a textarea for python:</p>
  <form role="form">
    <div class="form-group">
      <label for="comment">Comment:</label>
      <textarea class="form-control" rows="5" id="comment">
# indent your Python code to put into an email
import glob
# glob supports Unix style pathname extensions
python_files = glob.glob('*.py')
for file_name in sorted(python_files):
    print '    ------' + file_name

    with open(file_name) as f:
        for line in f:
            print '    ' + line.rstrip()

    print
</textarea>
    </div>
  </form>
</div>  
    <script>
      var myCodeMirror = CodeMirror.fromTextArea(document.getElementById('comment'), {
        mode: {name: "python",
          version: 2,
          singleLineStringErrors: false},
        lineNumbers: true,
        //indentUnit: 4,
        smartIndent: true,
        tabSize: 2,
        indentWithTabs: true,
        tabMode: "shift",
        autofocus: true,
        matchBrackets: true        
      });
    </script>
</body>
</html>
于 2015-07-14T18:33:27.607 回答