【问题标题】:CodeMirror textarea.getAttribute is not a function error in mvc3 applicationCodeMirror textarea.getAttribute is not a function error in mvc3 application
【发布时间】:2012-09-20 10:57:20
【问题描述】:

我在我的 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 不是函数
codemirror.js
2209线
textarea.getAttribute("autofocus") != null && hasFocus == document.body;

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

即使想,我在 JS 方面完全是个菜鸟,我想这很难做错,但我还是做了。

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

【问题讨论】:

  • code var a = CodeMirror.fromTextArea($code, { code 这是问题所在,应该是code CodeMirror.fromTextArea($code.get(0), ``code
  • 太棒了,正是我需要的提示。感谢发帖。
  • 只是为了编辑@inlines的评论观点:var a = CodeMirror.fromTextArea($code, {... 应该是CodeMirror.fromTextArea($code.get(0), ...
  • @inlines 注意英语中不使用反引号 (`),请使用撇号 (')。将防止您的格式被搞砸。所以it's,而不是it`s

标签: razor codemirror


【解决方案1】:

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

【讨论】:

  • 虽然这可能会回答问题,但提供更多信息和代码示例将显着提高其长期价值。请edit您的回答完成。
  • 海报表明他们是 JavaScript 新手,所以请解释一下 jQuery 查找的结果和 Document.getElementById() 返回的结果之间的区别(以及如何将一个转换为另一个)。跨度>
【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 2022-11-20
    • 1970-01-01
    • 2018-05-17
    • 2021-10-08
    相关资源
    最近更新 更多