日期:2014-05-16  浏览次数:20646 次

JS中的attachEvent、addEventListener如何传递参数
<!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