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