单击开始挂钩按钮时会立即调用函数 asyncMsg。仅当用户单击按我按钮时,如何更改为调用。我需要将 2 项数据传递到此回调中。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
function bindEvent(el, eventName, eventHandler) {
if (el.addEventListener){
el.addEventListener(eventName, eventHandler, false);
} else if (el.attachEvent){
el.attachEvent('on'+eventName, eventHandler);
}
}
function asyncMsg(data, fldid) {
alert("asynchronous message with param1=" + data + " param2=" + fldid);
}
function dohooking() {
var toinsert = "Duck";
var fldid = "10";
var btn = document.getElementById("hookee");
bindEvent(btn, 'click', asyncMsg(toinsert, fldid));
}
</script>
<title></title>
</head>
<body>
<input type="button" id="sethooking" value="start hooking" onclick="dohooking();">
<br />
<input type="button" id="hookee" value="press me">
<br />
</body>
</html>
我可以看到如果我只是传递函数,如下所示:
bindEvent(btn, 'click', asyncMsg);
然后它被异步调用,但是我该如何传递这两个参数呢?