class github-button中断addEventListener

时间:2016-07-07 15:09:46

标签: javascript github

我发现了一些有趣的尝试在Github按钮上添加Mixpanel分析:class =" github-button"因为我不理解的原因打破了任何addEventListener。

以下是代码示例:

<head>
    <script async defer src="https://buttons.github.io/buttons.js"></script>
</head>
<body>
        <div>
            <!-- Header -->
            <span class='text'>
                <a id="star" class="github-button" href="https://github.com/tnguyen42/FAQ.sh" data-icon="octicon-star" data-style="mega" data-count-href="/tnguyen42/FAQ.sh/stargazers" data-count-api="/repos/tnguyen42/FAQ.sh#stargazers_count" data-count-aria-label="# stargazers on GitHub" aria-label="Star tnguyen42/FAQ.sh on GitHub">Star</a>

                <a id="fork" class="github-button" href="https://github.com/tnguyen42/FAQ.sh/fork" data-icon="octicon-repo-forked" data-style="mega" data-count-href="/tnguyen42/FAQ.sh/network" data-count-api="/repos/tnguyen42/FAQ.sh#forks_count" data-count-aria-label="# forks on GitHub" aria-label="Fork tnguyen42/FAQ.sh on GitHub">Fork</a>
            </span>

            <span class='logoContainer'>
                <a class='logo'>FAQ</a>
            </span>

            <script type="text/javascript">
                var star = document.getElementById("star");
                var fork = document.getElementById("fork");

                function starClick() {
                    mixpanel.track('Star Click');
                    console.log("Star Click");
                }

                function forkClick() {
                    mixpanel.track('Fork Click');
                    console.log("Fork Click");
                }

                star.addEventListener("click", starClick, false);
                fork.addEventListener("click", forkClick, false);
            </script>
        </div>
</body>

现在,这段代码不起作用。 star被识别,fork被识别,但是来到addEventListener,我将永远不会得到任何应该执行的控制台日志。

现在,如果我只删除class="github-button",一切都会正常工作。知道为什么吗?

0 个答案:

没有答案