Js dynamically adds events and passes the parameter sample code
- 2020-03-26 21:34:21
- OfStack
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<rows.length; j++)
{
for(var i=0;i<rows[j].cells.length;i++)
{
rows[j].cells[i].attachEvent("onmousemove",tt(rows[j].cells[i]));
//rows[j].cells[i].onmousemove = function(){
// tt();
//}
}
}
}
========== ======= compatible with FF and IE
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
}
with Jquery In one sentence
$("input[type='text']").change( function() {
//You can write some validation code here
});