问题
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
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?
回答1:
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
}
来源:https://stackoverflow.com/questions/30819811/how-to-access-this-element-from-event-handler-passed-into-scalatags