事件绑定函数的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>

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-15
  • 2022-03-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-09
  • 2021-12-29
  • 2021-12-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案