Method 1:
VaR newopen = function (ID, level)
{
Return function ()
{
Opentree (ID, level); // This function is an execution function defined externally;
}
}
X. attachevent ("onclick", newopen (ID, parseint (level) + 1 ));
Y. attachevent ("onclick", newopen (ID, parseint (level) + 2 ));
Think8848 simplified Code :
If (X. addeventlistener) // If the browser is Firefox
{
X. addeventlistener ("click", function ()
{
Opentree (ID, level); // opentree is an execution function defined externally. Its ID and level are real parameters;
});
}
Else // the browser is IE
{
X. attachevent ("onclick", function ()
{
Opentree (ID, level );
});
}
Method 2:
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> </title>
</Head>
<Body>
<A href = "#" id = "mylink"> This is a connection </a>
<SCRIPT type = "text/JavaScript">
Function attachevent (type, target, handler, owner)
{
VaR eventhandler = handler;
If (owner)
{
Debugger;
// Redefine a processing function here
Eventhander = function (E)
{
Handler. Call (owner, e );
}
}
If(window.doc ument. All)
Target. attachevent ("On" + type, eventhander );
Else
Target. addeventlistener (type, eventhander, false );
}
Function mylink_click (E)
{
Alert (this. tagname );
VaR target = E. srcelement | e.tar get;
Alert (target. href );
}
Function Test ()
{
VaR mylink = your own Doc ument. getelementbyid ("mylink ");
Attachevent ("click", mylink, mylink_click, mylink );
}
</SCRIPT>
<Input type = "button" value = "attachevent" onclick = "test ()"/>
</Body>
</Html>