JS中的attachEvent、addEventListener如何传递参数_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > JS中的attachEvent、addEventListener如何传递参数

JS中的attachEvent、addEventListener如何传递参数

 2012/6/29 16:40:51  tsailer  程序员俱乐部  我要评论(0)
  • 摘要:<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>UntitledPage</title></head><
  • 标签:list Ten JS
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <a href="#" id="link1" aaa="a">超链接link1</a>
        <a href="#" id="link2" aaa="b">超链接link2</a>
        <input type="button" value="attachEvent" onclick="addEvent();" />
    </div>
    </form>
</body>
</html>

<script type="text/javascript">
function AttachEvent(target, eventName, handler, argsObject)
{
    var eventHandler = handler;
    if(argsObject)
    {
        eventHander = function(e)
        {
            handler.call(argsObject, e);
        }
    }
    if(window.attachEvent)//IE
        target.attachEvent("on" + eventName, eventHander );
    else//FF
        target.addEventListener(eventName, eventHander, false);
}


function LinkClick(e)
{
    alert(this.name);
//    var target = e.srcElement || e.target;
//    alert(target.getAttribute('aaa'));
}

function addEvent()
{
var obj = new Object();
obj.name = "link1111";
var link1 = document.getElementById("link1");
AttachEvent(link1, "click",  LinkClick, obj);

obj = new Object();
obj.name = "link2222";
var link2 = document.getElementById("link2");
AttachEvent(link2, "click",  LinkClick, obj);
}


</script>


转自
http://blog.csdn.net/avon520/archive/2008/10/06/3022363.aspx
发表评论
用户名: 匿名