JS打印页面绑定事件和点击事件

<html>
<head>
    <style>
        .debugLog {
            font-size: 14px;
        }
    </style>
    <script>
        (function () {
            window.LogDivObj = document.createElement("div");
            window.LogDivObj.className = "debugLog";

            var htmlElement = HTMLElement.prototype;
            var oldAddEventListener = htmlElement.addEventListener;

            function getElementId(dom) {
                var id = dom.id,
                        className = dom.className,
                        tagName = dom.tagName;
                return tagName + (id ? "#" + id : (className ? "." + className : ""));
            }

            var proxy = function () {

                window.LogDivObj.innerHTML = window.LogDivObj.innerHTML + "<br/>bind " + getElementId(this) + " : " + arguments[0];
                var fun = arguments[1];
                arguments[1] = function () {
                    window.LogDivObj.innerHTML = window.LogDivObj.innerHTML + "<br/>fire " + getElementId(arguments[0].srcElement) + " : " + arguments[0].type;
                    var result = fun.apply(this, arguments);
                    return result;
                }
                var result = oldAddEventListener.apply(this, arguments);
                return result;
            }
            htmlElement.addEventListener = proxy;
        })();
    </script>
    <script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("div").delegate("button", "click", function () {
                $("p").slideToggle();
            });
        });
    </script>
</head>
<body>
<div style="background-color:red">
    <p>这是一个段落。</p>
    <button>请点击这里</button>
</div>
<script>
    document.body.appendChild(window.LogDivObj);
</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/yangzl2008/article/details/42272097