如何从传递给scalatags的事件处理程序访问'this'元素? [英] How to access 'this' element from event handler passed into scalatags?
问题描述
我正在尝试从使用scalatags创建的事件处理程序中访问当前(this
)元素的文本.这是我尝试过的:
I'm trying to access the text of the current (this
) element from within an event handler created with scalatags. Here is what I tried:
val onChange = {(e: HTMLElement) =>
number() = e.textContent.toInt
}: js.ThisFunction
input(`type`:="number", onchange := onChange).render
当我调试上面的代码时,没有任何内容传递给onChange
函数.具体来说,如果我将其放入函数主体:js.Dynamic.global.alert(JSON.stringify(e))
,它将显示{}
.另外,我得到一个错误,指出e.textContent
为空.如何获取它传递给javascript this
元素?
When I debug the above code, nothing is being passed into the onChange
function. Specifically, if I put this into the function body: js.Dynamic.global.alert(JSON.stringify(e))
, it prints {}
. Also, I get an error that e.textContent
is null. How do I get it to pass in the javascript this
element?
推荐答案
我对 scala有了一些澄清.js gitter ,事实证明,您可以像这样从闭包内部访问元素:
I got some clarification on scala.js gitter, and it turns out you can access the element from within a closure like so:
val inputElem = input(`type`:="number").render
inputElem.onchange = {(e: Event) =>
number() = inputElem.value.toInt
}
这篇关于如何从传递给scalatags的事件处理程序访问'this'元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!