Razor CodeMirror textarea.getAttribute不是mvc3应用程序中的函数错误

Razor CodeMirror textarea.getAttribute不是mvc3应用程序中的函数错误,razor,codemirror,Razor,Codemirror,我在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" } }) var a = CodeMirror.from

我在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" } })
var a = CodeMirror.fromTextArea($code, {
        lineNumbers: true,
        matchBrackets: true,
        mode: "text/x-csharp"
});
其中,
$code

var $code = jQuery('.nffp-code', $root);
页面加载后,我出现以下错误:

TypeError:textarea.getAttribute不是函数
codemirror.js
第2209行
textarea.getAttribute(“自动对焦”)!=null&&hasFocus==document.body

我使用本手册使用CodeMirror:

即使这样想,我在JS中是个十足的傻瓜,我想很难做错事,但我还是做了


您知道如何解决这个问题吗?

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

document.getElementById('contents'); //returns a HTML DOM Object

var contents = $('#contents');  //returns a jQuery Object
在jQuery中,要获得与
document.getElementById()
相同的结果,可以访问jQuery对象并获取对象中的第一个元素(记住JavaScript对象的行为类似于关联数组)


code
var a=CodeMirror.fromTextArea($code,{
code
这就是问题所在,应该是
code
CodeMirror.fromTextArea($code.get(0))`codeBrilliant,这正是我需要的提示。谢谢发布。只需编辑@inlines的评论视图:
var a=CodeMirror.fromTextArea($code,{…
应该是
codemirr.fromTextArea($code.get(0),…
@inlines注意英文中不使用反勾(`),请使用撇号(`)因此,它是,而不是。虽然这可能会回答这个问题,但提供额外的信息和代码示例将显著提高其长期价值。请填写您的答案。海报表明他们是JavaScript新手,因此请解释e jQuery查找结果与
Document.getElementById()
返回的内容之间的区别(以及如何将两者转换为另一种)。
var contents = $('#contents')[0]; //returns a HTML DOM Object