CodeMirror textarea.getAttribute is not a function error in mvc3 application

你说的曾经没有我的故事 提交于 2019-12-01 01:50:42

问题


I'm using CodeMirror in my ASP.NET MVC 3 application, CodeMirror's version is up to date(2.34)

my textarea looks like this:

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

I use CodeMirror like this:

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

where $code is

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

And after page load I have this error:

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

I used this manual for using CodeMirror: manual

Even thought, I'm a total noob in JS, I guess it's hard to do it wrong, still I did.

Any Ideas how to fix the problem?


回答1:


You need to use document.getElementById() instead of the jQuery lookup.




回答2:


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

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

In jQuery, to get the same result as document.getElementById(), you can access the jQuery Object and get the first element in the object (Remember JavaScript objects act similar to associative arrays).

var contents = $('#contents')[0]; //returns a HTML DOM Object


来源:https://stackoverflow.com/questions/12511208/codemirror-textarea-getattribute-is-not-a-function-error-in-mvc3-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!