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

标签 javascript jquery prototypejs

我尝试将我的代码从 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 还是 元素。原型(prototype)使用猴子修补,而不是包装,因此您可以直接使用裸元素。)


转换后的代码:

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);
});

关于javascript - Prototype.js - $(...).next 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53390687/

相关文章:

javascript - JS中如何获取JSON的值

javascript - JavaScript 中的范围错误。在该对象的不同方法中调用方法

javascript - 为什么我需要 setTimeout.bind(window)

javascript - Chrome 没有在我的 PWA 中显示 "add to homescreen"

jquery - 使用 cookie 永久更改 CSS

c# - 切换选项卡后日期选择器不起作用

javascript - 修改复选框的checked属性无法正常工作

javascript - 使用 jQuery 在 textarea 中设置最大长度

javascript - 多个引导日期选择器在同一页面上具有扩展形式

javascript - 如何使用ajax设置选定的选项?