Kotlin gets jquery click target

If I write the following code, I get a compiler error: "Unresolved refrence: target"

jq("#element").click { console.log(it.target) } 

However, if I print only β€œthis”, it has the target target

 r.Event {originalEvent: MouseEvent, type: "click", target: button, currentTarget: button, relatedTarget: null…} 

How did I get support to get the goal?

+5
source share
1 answer

Suppose you are using jq from the standard library, and above all, jq from the standard library is deprecated.

Then consider the definition of a click handler:

 public fun click(handler: (MouseClickEvent) -> Unit): JQuery 

As you can see, it in your case MouseClickEvent . But MouseClickEvent and MouseEvent does not contain target .

You can write your own bindings for jquery:

 import jquery.MouseClickEvent import jquery.MouseEvent @JsName("$") public external fun jq(selector: String): JQuery public external class JQuery() { public fun click(handler: (ExtendedMouseClickEvent) -> Unit): JQuery } public external class ExtendedMouseClickEvent() : MouseEvent { public val target: JQuery public val which: Int } fun main(args: Array<String>) { jq("#element").click { console.log(it.target) } } 

In addition, you can convert existing definitions for TypeScript to kotlin.

jQuery: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/jquery

ts2kt: https://github.com/Kotlin/ts2kt

+1
source

Source: https://habr.com/ru/post/1265242/


All Articles