Home > Web Front-end > JS Tutorial > body text

js动态添加事件并可传参数示例代码_javascript技巧

WBOY
Release: 2016-05-16 17:19:20
Original
1322 people have browsed it
复制代码 代码如下:

var tt=function(obj)
{
return function()
{
alert(obj.tagName); //可以为外部定义的一个执行函数;
}
}
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();
//}
}
}
}

==========兼容FF和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

}

用Jquery的话,一句话搞定
$("input[type='text']").change( function() {
// 这里可以写些验证代码
});
Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!