[英]listen to click event JS
I tried to listen to click event in Haxe/JS: 我试图听Haxe / JS中的click事件:
static function main()
{
var initHandler:Dynamic = init;
Browser.window.onload = initHandler;
}
static private function init()
{
var clickHandler:Dynamic = learnBtn;
Browser.document.getElementById("readMoreBtn").addEventListener("click", clickHandler);
}
static private function learnBtn()
{
}
I'm really need all this? 我真的需要这一切吗?
Why i can't do this (without initHandler
and clickHandler
): 为什么我不能做到这一点(没有
initHandler
和clickHandler
):
Browser.window.onload = init;
OR 要么
Browser.document.getElementById("readMoreBtn").addEventListener("click", learnBtn);
The JS file not loaded after all the HTML? 所有的HTML都没有加载JS文件吗? I can to define that?
我可以定义吗?
It's not related to this topic but I tried to google and search in Haxe.org but nothing. 它与本主题无关,但是我尝试在Haxe.org上搜索和搜索,但是什么也没有。 what it is "untyped" keyword.
什么是“无类型”关键字。
Using a Dynamic
intermediary variable indirectly solves your problem but you should try understanding the compiler error message. 间接使用
Dynamic
中介变量可以解决您的问题,但是您应该尝试理解编译器错误消息。
What the compiler says: 编译器怎么说:
If you write: 如果您写:
function init() {
// do stuff on window.onload
}
Browser.window.onload = init;
the compiler will tell you: 编译器会告诉您:
Test.hx:22: characters 68-117 : Void -> Void should be js.html.EventListener
Test.hx:22: characters 68-117 : Void -> Void should be Dynamic -> Void
What does that mean? 这意味着什么? It means that your function
init()
, which has a Void->Void
signature, should be js.html.EventListener
, which is defined as Dynamic->Void
. 这意味着具有
Void->Void
签名的函数init()
应该是js.html.EventListener
,其定义为 Dynamic->Void
。
The right code: 正确的代码:
In other words you init()
function should have an argument so it should be declared as: 换句话说,您的
init()
函数应具有一个参数,因此应将其声明为:
function init(event:Dynamic) {
// do stuff on window.onload
}
Forcing the compiler 强制编译器
You can tell the compiler to let you write possibly incorrect (but we know valid) code: 您可以告诉编译器让您编写可能不正确(但我们知道有效)的代码:
var foo:Dynamic = init;
Browser.window.onload = foo; // always happy
Browser.window.onload = untyped init; // do not type, please
Browser.window.onload = cast init; // I'm sure I'm right
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.