【问题标题】:TinyMCE - Uncaught TypeError: wc[0].removeClass is not a functionTinyMCE - 未捕获的类型错误:wc[0].removeClass 不是函数
【发布时间】:2016-01-12 12:11:07
【问题描述】:

This TinyMCE plugin 是从 wordcount 插件派生的,当字符数超过设置限制时也会显示错误消息。但它不起作用。在每次更新操作时,addClassremoveClass 都找不到函数。我稍微编辑了代码,但核心是一样的:

// on init
var statusbar = editor.theme.panel && editor.theme.panel.find('#statusbar')[0];
if (statusbar) {
    tinymce.util.Delay.setEditorTimeout(editor, function() {
        statusbar.insert({
            type: 'label',
            name: 'maxlength',
            text: ['Length: {0}', self.getCharCount()],
            classes: 'wordcount',
            disabled: editor.settings.readonly
        }, 0);
...
// on update
var wc = editor.theme.panel.find('#maxlength');
wc[0].removeClass('danger');  // Error!

如何解决?

编辑: console.log(wc[0]) 输出:

t {_super: undefined, settings: Object, _id: "mceu_160", _aria: Object, _elmCache: Object…}
  $: function f(e,t)
    arguments: [Exception: TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context...]
    attrHooks: Object
    caller: [Exception: TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context...]
    contains: function (e,t)
    cssHooks: Object
    each: function m(e,t)
    expr: Object
    extend: function s(e,n)
    filter: function (e,t,n)
    find: function e(e,t,n,r)
    fn: f[0]
    grep: function g(e,t)
    inArray: function h(e,t)
    isArray: function isArray()
    length: 2
    makeArray: function (e)
    name: "f"
    overrideDefaults: function (e)
    prototype: f[0]
    text: function (e)
    trim: function p(e)
    unique: function (e)
    __proto__: function ()
    <function scope>
  $el: f.fn.f.init[1]
  _aria: Object
  _elmCache: Object
  _eventDispatcher: t
  _eventsRoot: t
  _id: "mceu_160"
  _name: "wordcount"
  _nativeEvents: Object
  _parent: t
  _super: undefined
  borderBox: undefined
  canFocus: false
  classes: n
  data: t
  marginBox: undefined
  paddingBox: undefined
  rootControl: t
  settings: Object
  state: t
  type: "label"
  __proto__: t

【问题讨论】:

  • editor.theme.panel.find('#maxlength') 是一个有效的 dom 元素吗?
  • 尝试记录 wc 变量并查看它包含的内容。除此之外,请确保 jQuery 已正确加载:)
  • @Thariama 是的,是的。
  • @Ynhockey,请查看我对问题的编辑。

标签: javascript tinymce tinymce-plugins


【解决方案1】:

我认为删除答案包含了这个问题的关键。

wc[0] 拥有一个有效的 dom 元素,但 removeClasse 不是一个您可以在此类元素上调用的有效函数(至少不使用 jQuery)。

如果不使用 jQuery,您可以尝试以下操作:

而不是调用removeClass 使用

wc[0].setAttribute('class', '');

而不是调用addClass 使用

wc[0].setAttribute('class', 'danger');

这有点简化,但如果您没有在 wc[0] 元素中使用任何其他类,它应该没问题。

【讨论】:

  • 同样的错误:wc[0].setAttribute is not a function
  • 嗯,你能告诉我你尝试执行代码的确切位置吗(你说'onInit',但这是什么意思)?
  • 请参考这些sources - update() 内部有addClass / removeClass 方法; update()keyupsetcontent beforeaddundo TinyMCE 事件上执行。
  • 嗯,看起来非常好。你在哪里调用 console.log(wc[0]);
  • 在 Chrome 的 JS 控制台中;我在var wc 初始化后放置了一个断点并调用了console.log(wc[0]);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-19
  • 1970-01-01
  • 2019-06-06
  • 2019-05-24
  • 2021-12-15
相关资源
最近更新 更多