CodeMirror和Bootstrap 2.3之间的Textarea冲突

时间:2013-11-12 13:28:52

标签: twitter-bootstrap textarea codemirror twitter-bootstrap-2

我正在尝试使用CodeMirror在textarea中进行语法突出显示。我也在使用Bootstrap 2.3作为textarea。我得到了Bootstrap显示的textarea中的代码,但是在CodeMirror中没有语法突出显示,我在Chrome控制台中收到此错误:

未捕获的TypeError:无法设置未定义的属性“display”。

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

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
  });

1 个答案:

答案 0 :(得分: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>