javascript - 如果我可以在源代码中看到我的工具提示,为什么它没有显示?

标签 javascript svg d3.js tooltip

我已经编写了一个显示图表的小网页,但现在我在显示工具提示时遇到问题。它已正确附加到 DOM,但我在页面上看不到它。

 d3.selectAll("circle")
        .on("mouseover", function(d){
            d3.select(this)
                .transition()
                .attr("r", circle_size_hover)
                .duration(transition_duration)
                .style("opacity", 1);

            d3.select(this)
                .append("div")
                    .attr("class", "mytooltip")
                    .text(d.alarms)
                    .transition()
                    .style("opacity", 1);

            console.log(d.alarms);
        });

之后我可以在 DOM 中看到我的 div:

<g class="circles">
    <circle cx="79.34074074074073" cy="113.50243902439024" r="7" style="opacity: 0.7;">
    <div class="mytooltip">51.28205128205128</div>
    </circle>
</g>

CSS:

.mytooltip {
    position: absolute;           
    text-align: center;           
    width: 60px;                  
    height: 28px;                 
    padding: 2px;             
    font: 12px sans-serif;        
    background: lightsteelblue;   
    border: 0px;      
    border-radius: 8px;           
    pointer-events: none;         
}

JSFiddle:http://jsfiddle.net/L42LU/4/

最佳答案

您可以直接将 html 元素添加到正文并按如下方式放置工具提示

d3.select("body").append("div").attr("class","tooltip")
                                .style("top",(d3.event.clientY - 10) + "px")
                                .style("left",(d3.event.clientX + 10) + "px")
                                .style("z-index", "10")
                                .style("visibility", "visible")
                                .text(d.alarms);

以上工具提示将显示在鼠标指针位置。 同样,在鼠标悬停事件中,您只需更改可见性样式即可隐藏此工具提示。

工具提示 CSS:

div.tooltip {
        position: absolute;
        text-align: left;
        padding: 8px;
        font: 12px Verdana;
        background: lightsteelblue;
        border: 0px;
        border-radius: 8px;
        pointer-events: none;
    }

关于javascript - 如果我可以在源代码中看到我的工具提示,为什么它没有显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18551432/

相关文章:

python : call a function with n arguments

javascript - Datamap 的 D3 气泡不显示

javascript - 从两个不同的对象获取对应的值

javascript - 在发送到 IBM Watson Assistant 之前删除 Node.js 应用程序中的上下文变量

javascript - 在javascript中选择一系列数字的算法

Javascript 简单的面向对象

ios - 如何在 IOS 中为 SVG 路径设置动画?

javascript - AmCharts 每周数据展示

javascript - 从辅助 Y 轴上的值中删除负号(Nvd3 - 多条水平图表)

javascript - 如何从外部 JSON 文件设置正确的日期?