【发布时间】: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