兼容IE9以下和非IE浏览器的原生js事件绑定函数

事件绑定函数的demo如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta charset="UTF-8"/>
    <title>测试事件绑定函数</title>
    <script> window.onload = function(){ var liObj, ulObj; ulObj = document.getElementsByTagName('ul')[0]; addEvent(ulObj, 'click', clickObjLi) }; //兼容IE和非IE浏览器 function addEvent(ele, event_name, func){ if(window.attachEvent){ ele.attachEvent(event_name, func); } else{ event_name = event_name.replace(/^on/, ""); ele.addEventListener(event_name, func, false); //默认事件是冒泡 } } function clickObjLi (event){ var currTarget = event.target; alert(currTarget.innerHTML); } </script>
</head>
<body>
    <div>
        <ul>
            <li class="item1">测试事件绑定1</li>
            <li class="item2">测试事件绑定2</li>
            <li class="item3">测试事件绑定3</li>
            <li class="item4">测试事件绑定4</li>
        </ul>
    </div>
</body>
</html>

你可能感兴趣的:(浏览器)