【问题标题】:JQuery replaceWith in CoffeescriptCoffeescript 中的 JQuery replaceWith
【发布时间】:2015-01-16 23:58:27
【问题描述】:

我在使用 replaceWith 将按钮替换为另一个标签时遇到了一些问题。我的代码:

<button name="button" type="button" class="join-button" 
        data-player="1">Join Game!</button>
buttonToText = (element) ->
  console.log(element)
  element.replaceWith( "<h1>hi</h1>" )

$ ->
  $(".join-button").on click:  ->
    buttonToText(this)

单击按钮时,console.log 出现返回正确的值:按钮的 html 和类是 HTMLButtonElement。但是,我收到Uncaught Type Error: Undefined is Not a Function 在替换行上。任何想法为什么会发生错误?如果有什么不同的话,这一切都在 Rails 4.2 之外运行。

【问题讨论】:

  • 另外,replaceWith 似乎不在元素的Object.getOwnPropertyNames() 列表中,所以我想我不能在这里使用replaceWith?
  • this 在您的回调中将是一个 DOM 节点,对吧?但是replaceWith 是一个 jQuery 函数,而不是标准的 DOM 节点函数。也许$(element) 会更有用。
  • 对! $(this) 而不是 this$(element) 有效!谢谢!

标签: jquery coffeescript replacewith


【解决方案1】:

this 需要是$(this)element 需要是$(element)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    相关资源
    最近更新 更多