Javascript code for dynamically adding events and passing parameters _ javascript tips-js tutorial

Source: Internet
Author: User
Many related articles can be found in js dynamic addition events, but few parameters can be passed. Below is a good example to meet your requirements for parameter passing, do not miss out. The Code is as follows:


Var tt = function (obj)
{
Return function ()
{
Alert (obj. tagName); // an execution function that can be defined externally;
}
}
Function addfunction ()
{
Var bigobj = document. getElementById ("mytable ");
Var rows = bigobj. rows;
For (var j = 0; j {
For (var I = 0; I {
Rows [j]. cells [I]. attachEvent ("onmousemove", tt (rows [j]. cells [I]);
// Rows [j]. cells [I]. onmousemove = function (){
// Tt ();
//}
}
}
}


=========== Compatible with FF and IE

The Code is as follows:


Function addEvent (o, c, h ){
If (o. attachEvent ){
O. attachEvent ('on' + c, h );
} Else {
O. addEventListener (c, h, false );
}
Return true ;}
Var tt = function (obj)
{
Return function () {textChange (obj );}
}
AddEvent (input1, "change", tt (input1 ));
Function textChange (o)
{

// Do something

}

If Jquery is used, it can be done in one sentence.
$ ("Input [type = 'text']"). change (function (){
// Here you can write some verification code
});

Related Article

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.