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

时间:2012-09-20 10:57:21

标签: razor codemirror

m using codemirror in my ASP.NET MVC 3 application, 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(“autofocus”)!= null&amp;&amp; hasFocus == document.body;

我使用本手册来使用codemirror: manual

即便如此,我m a total noob in JS, I guess it很难做错,我仍然这样做。

任何想法如何解决问题?

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:2)

class Worker : public QObject
{
    Q_OBJECT

public slots:
    void doWork(const QString &parameter) {
        QString result;
        /* ... here is the expensive or blocking operation ... */
        emit resultReady(result);
    }

signals:
    void resultReady(const QString &result);
};

class Controller : public QObject
{
    Q_OBJECT
    QThread workerThread;
public:
    Controller() {
        Worker *worker = new Worker;
        worker->moveToThread(&workerThread);
        connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
        connect(this, &Controller::operate, worker, &Worker::doWork);
        connect(worker, &Worker::resultReady, this, &Controller::handleResults);
        workerThread.start();
    }
    ~Controller() {
        workerThread.quit();
        workerThread.wait();
    }
public slots:
    void handleResults(const QString &);
signals:
    void operate(const QString &);
};

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

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

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