Prototype.js - $(...).next 不是函数

Prototype.js - $(...).next is not a function

我尝试将我的代码从 jQuery 转换为 prototype.js。我执行以下代码,但得到 $(...).next is not a function

var editorCounter = 0;
var newEditorIds = [];

$$(".input-text.mceEditor").each(function() {
    var next = $(this).next();
    var tagName = next.prop("tagName");

    if (typeof(tagName) == "undefined") {
        var newId = "newEditor_" + editorCounter;
        $(this).attr("id", newId);
        newEditorIds.push(newId);
        editorCounter++;
    }
});

newEditorIds.each(function(name, index) {
    tinymce.EditorManager.execCommand('mceAddEditor', true, name);
});

未完全转换为prototype.js。我仍然需要找出 prop()attr() 的等价物。 不过,我不明白到目前为止我做错了什么,因为我告诉自己 this site 它应该有效。


原始工作jQuery代码:

var editorCounter = 0;
var newEditorIds = [];

jQuery(".input-text.mceEditor").each(function() {
    var next = jQuery(this).next();
    var tagName = next.prop("tagName");

    if (typeof(tagName) == "undefined") {
        var newId = "newEditor_" + editorCounter;
        jQuery(this).attr("id", newId);
        newEditorIds.push(newId);
        editorCounter++;
    }
});

jQuery.each(newEditorIds, function(i, v) {
    tinymce.EditorManager.execCommand('mceAddEditor', true, v);
});
您正在使用的

Array.prototype.each 未设置 this。您应该在回调函数中提供一个参数来接收元素。因此:

$$(".input-text.mceEditor").each(function(element) {
    var next = element.next();

(您可以使用 $(element),但它不会做任何事情,除非您不知道 element 是 ID 还是 Element。原型使用 monkey-修补,而不是包装,所以你可以直接使用裸机 Element。)


转换后的代码:

var editorCounter = 0;
var newEditorIds = [];

$$(".input-text.mceEditor").each(function(element) {
    var next = element.next();

    if (typeof(next) == "undefined") {
        var newId = "newEditor_" + editorCounter;
        element.id = newId;
        newEditorIds.push(newId);
        editorCounter++;
    }
});

newEditorIds.each(function(name, index) {
    tinymce.EditorManager.execCommand('mceAddEditor', true, name);
});