setAttribute, onClick and cross browser compatibility

前端 未结 4 1296
天涯浪人
天涯浪人 2020-12-02 02:21

I have read a number of posts about this but none with any solid answer. Here is my code:

// button creation
onew = document.createElement(\'input\');
onew.         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 02:28

    Use the addEventListener() function with "click" for the type argument for Mozilla-based browsers, and attachEvent() function with "onclick" as the sEvent argument for IE; I find it best to use a try/catch statement, for example:

    try {
      onew.attachEvent("onclick", //For IE
                       function(){fnDisplay_Computers("'" + alines[i] + "'"); });
    }
    catch(e) {
      onew.addEventListener("click", //For Mozilla-based browsers
                       function(){fnDisplay_Computers("'" + alines[i] + "'"); },
                       false);
    }
    

提交回复
热议问题