【问题标题】:Prototype.js - $(...).next is not a functionPrototype.js - $(...).next 不是函数
【发布时间】:2019-04-22 18:15:12
【问题描述】:

我尝试将我的代码从 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);
});

【问题讨论】:

  • 你的意思是说原始 jQuery 代码:工作正常但prototype.js 不工作?
  • 没错@NegiRox
  • 试试 $$(this).next();
  • $$ 充当querySelectorAll - 接受选择器并给出Array$ 充当 getElementById - 获取 ID 并提供 Element
  • 顺便说一句,writeAttribute 是普通香草setAttribute 的更强大版本。没有与prop 等效的东西——只需直接在Element 对象上使用属性。即,不要说$(element).prop('checked', true),而是说element.checked = true

标签: javascript jquery prototypejs


【解决方案1】:

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

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

(你可以使用$(element),但它不会做任何事情,除非你不知道element是一个ID还是Element。原型使用猴子补丁,而不是包装,所以你可以直接使用裸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);
});

【讨论】:

  • @Black 提供的更正代码。 (抱歉回滚,我看错了东西,一秒钟还以为不对,冲动地回滚了。)
猜你喜欢
  • 2018-12-10
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-21
  • 2017-08-10
相关资源
最近更新 更多