Javascript transmits parameters for the event Processor

Source: Internet
Author: User
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.