事件委托

2019-11-13  本文已影响0人  郭海杰

```

<script>

window.onload = function () {

        var addEvent = (function (...args) {

            getEvent.call(this)

            if (window.addEventListener) {

                return function (...args) {

                    ele.addEventListener(type, getEvent)

                }

            } else if (window.attachEvent) {

                return function (...args) {

                    ele.attachEvent("on" + type, getEvent)

                }

            }

        })()

        var ele = document.getElementsByClassName('Oul')[0]

        var type = "click"

        addEvent(ele, type, getEvent)

        function getEvent(e) {

            if (e) {

                console.log(e.target)

            }

        }

    }

</script>

```

上一篇 下一篇

猜你喜欢

热点阅读