Plain JavaScript tooltip

前端 未结 5 2023
渐次进展
渐次进展 2020-12-08 01:16

I am trying to make a tooltip in plain JavaScript which is shown on hover. Like the one in Stack Overflow on hover over the profile name a div is s

5条回答
  •  无人及你
    2020-12-08 01:33

    Even for $(document).ready, it’s hard to accomplish in pure JS—see here: $(document).ready equivalent without jQuery

    So I’m using a simple version:

    window.addEventListener("load", function () {
        var couponcodes = document.getElementsByClassName("couponcode");
        for (var i = 0; i < couponcodes.length; i++) {
            couponcodes[i].addEventListener("mouseover", function () {
                var coupontooltip = this.getElementsByClassName("coupontooltip")[0];
                coupontooltip.removeAttribute("style");
            });
            couponcodes[i].addEventListener("mouseout", function () {
                var coupontooltip = this.getElementsByClassName("coupontooltip")[0];
                coupontooltip.style.display = "none";
            });
        }
    });
    

    http://jsfiddle.net/mynetx/5qbP3/

提交回复
热议问题