• FireFox下为元素附加事件并传递参数-addEventListener attachEvent Pass parameters to eventfunction


    But you can get information of the object where the event occured. E.g. you can do stuff dependent on the "id" of an object, or you can set any additional flags within the object and do stuff dependent on these flags.

    Here we have the code for attaching the events:
    if(window.addEventListener){ // Mozilla, Netscape, Firefox
    object.addEventListener('mouseover', testevent, false);
    object.addEventListener('click', testevent, false);
    object.myflag = "test";
    object.mydata = "123";
    } else { // IE
    object.attachEvent('onmouseover', testevent);
    object.attachEvent('onclick', testevent);
    object.myflag = "test";
    object.mydata = "123";
    }
    

    The function "testevent" contains all code to access the "id", "name" etc. and all own flags:
    function testevent(evt){
    var e_out;
    var ie_var = "srcElement";
    var moz_var = "target";
    var prop_var = "myflag";
    // "target" for Mozilla, Netscape, Firefox et al. ; "srcElement" for IE
    evt[moz_var] ? e_out = evt[moz_var][prop_var] : e_out = evt[ie_var][prop_var];
    alert(e_out);
    prop_var = "mydata";
    evt[moz_var] ? e_out = evt[moz_var][prop_var] : e_out = evt[ie_var][prop_var];
    alert(e_out);
    }
    


    Conclusion: If you set appropriate flags when attaching an event to an object, you have the possiblity to do actions dependent on these flag when the actual event occurs.
    Therefore it is not necessary to pass parameters directly when attaching the event, since those parameters either do not change anyway, or if the parameters are dynamically created, those dynamic parameters can be read out in the function "testevent".
  • 相关阅读:
    ArrayList的subList方法
    easyui-datagrid 的loader属性用法
    easyui-datagrid 编辑模式详解
    C++归并排序总结
    2016阿里巴巴笔试题
    leetCode(38):Lowest Common Ancestor of a Binary Search Tree
    Codeforces Round #271 (Div. 2) 解题报告
    Android平台Camera实时滤镜实现方法探讨(九)--磨皮算法探讨(一)
    Docker部署JavaWeb项目实战
    广东省知名P2P平台资料
  • 原文地址:https://www.cnblogs.com/goody9807/p/1163992.html
Copyright © 2020-2023  润新知