9

我在我的 ASP.NET MVC 3 应用程序中使用 CodeMirror,CodeMirror 的版本是最新的(2.34)

我的textarea样子是这样的:

@Html.TextAreaFieldFor(s => s.Data.CodeBehind, htmlAttributes: new Dictionary<string, object> { { "class", "textbox codeBehind nffp-code" } })

我像这样使用 CodeMirror:

var a = CodeMirror.fromTextArea($code, {
        lineNumbers: true,
        matchBrackets: true,
        mode: "text/x-csharp"
});

$code在哪里

var $code = jQuery('.nffp-code', $root);

页面加载后我有这个错误:

TypeError: textarea.getAttribute is not a function
codemirror.js
Line 2209
textarea.getAttribute("autofocus") != null && hasFocus == document.body;

我使用本手册来使用 CodeMirror: 手册

甚至认为,我是 JS 的菜鸟,我想这很难做错,但我还是做到了。

任何想法如何解决这个问题?

4

2 回答 2

9

您需要使用document.getElementById()而不是 jQuery 查找。

于 2016-04-25T09:39:33.093 回答
2
document.getElementById('contents'); //returns a HTML DOM Object

var contents = $('#contents');  //returns a jQuery Object

在 jQuery 中,要获得与 相同的结果document.getElementById(),您可以访问 jQuery 对象并获取对象中的第一个元素(请记住 JavaScript 对象的行为类似于关联数组)。

var contents = $('#contents')[0]; //returns a HTML DOM Object
于 2017-08-16T17:22:25.333 回答