【问题标题】:Display tooltip from JavaScript.push method从 JavaScript.push 方法显示工具提示
【发布时间】:2014-08-19 22:30:08
【问题描述】:

我正在观看有关创建工具提示的 youtube 视频 (http://www.youtube.com/watch?v=gJY8YUjFd58),我能够将所有内容都放在一个平面 html 文件中,其中所有内容都内联并具有单独的 js/css。但是我一直没能做到的是使用 JavaScript 推送方法在 html 页面中显示相同的数据。

我正在使用 JavaScript 将 HTML 表格和内联 CSS 发送到 HTML 主页面 (tt_hvr.html)。 CSS 似乎可以工作,但我无法显示元素内的悬停文本。我已经发布了文件。有没有人对如何让悬停显示 div 内的文本有任何想法?我在下面发布了三个文件(tt_hvr.html、tt_hvr.js、tt_hvr1.js)。提前致谢。

tt_hvr.html

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>

<body>
<p id='data_here'></p>

</body>
<script src='tt_hvr.js' type="text/javascript" charset="utf-8"></script>
<script src='tt_hvr1.js' type="text/javascript" charset="utf-8"></script>
<script src='http://code.jquery.com/jquery-1.9.1.js' type="text/javascript" charset="utf-8"></script>

</html>

tt_hvr.js

   var hvrinfo = [];
   hvrinfo.push("<style type='text/css'>");
   hvrinfo.push("td {color: sienna;}");

   //Testing the tooltip begin.
   hvrinfo.push("   .hvrtip0{");
   hvrinfo.push("       margin-left:auto;");
   hvrinfo.push("       margin-right:auto;");
   hvrinfo.push("       margin-top:auto;");
   hvrinfo.push("       /*width:250px; */");
   hvrinfo.push("       background-color:#ffffff;");
   hvrinfo.push("       color:#000000;");
   hvrinfo.push("       padding:auto;");
   hvrinfo.push("       text-align:left;");
   hvrinfo.push("       }");
   hvrinfo.push("   .tooltip00{");
   hvrinfo.push("       position:absolute; /*Allows it to be anywhere on the page  without interrupting any other elements on the page.*/");
   hvrinfo.push("       z-index:2;");
   hvrinfo.push("       width:auto;");
   hvrinfo.push("       padding:5px;");
   hvrinfo.push("       background-color:#ffff00;");
   hvrinfo.push("       border:2px solid #000000;");
   hvrinfo.push("       border-radius:15px; //Rounding the corners on the box.");
   hvrinfo.push("       -moz-border-radius:15px; /*Firefox*/");
   hvrinfo.push("       display:none; /*Hide from page so that it will only be shown on hover.*/");
   hvrinfo.push("       }");
   hvrinfo.push("   td.hvrtip0{");
   hvrinfo.push("       background-color:#c0c0c0;");
   hvrinfo.push("       border:2px solid #e0e0e0;");
   hvrinfo.push("   }");
   hvrinfo.push("   table,td,th");
   hvrinfo.push("   {");
   hvrinfo.push("       border:1px solid black;");
   hvrinfo.push("   }");
   hvrinfo.push("   table");
   hvrinfo.push("   {");
   hvrinfo.push("       width:50%;");
   hvrinfo.push("       border-collapse:collapse;");
   hvrinfo.push("   }");
   hvrinfo.push("   th");
   hvrinfo.push("   {");
   hvrinfo.push("       height:50px;");
   hvrinfo.push("   }");
   hvrinfo.push("   #redthis");
   hvrinfo.push("   {");
   hvrinfo.push("       color:red;");
   hvrinfo.push("   }");
   //Testing the tooltip end.

   //Pushing the table begin.
   hvrinfo.push("</style>");
   hvrinfo.push("<table border='1'><tr><th>Row1</th><th>Row2</th><th>Row3</th></tr>");
   hvrinfo.push("<tr><td class='hvrtip0'>Cell 01</td><td>Cell 02</td><td>Cell 03</td></tr>");
   hvrinfo.push("<tr><td>Cell 04</td><td>Cell 05</td><td>Cell 06</td></tr>");
   hvrinfo.push("<tr><td>Cell 07</td><td>Cell 08</td><td class='hvrtip0'>Cell 09</td></tr>");
   hvrinfo.push("<tr><td>Cell 10</td><td>Cell 11</td><td>Cell 12</td></tr>");
   hvrinfo.push("<tr><td>Cell 13</td><td>Cell 14</td><td>Cell 15</td></tr>");
   hvrinfo.push("<tr><td>Cell 16</td><td>Cell 17</td><td>Cell 18</td></tr>");
   hvrinfo.push("<tr><td>Cell 19</td><td>Cell 20</td><td>Cell 21</td></tr>");
   hvrinfo.push("<tr><td>Cell 22</td><td>Cell 23</td><td>Cell 24</td></tr>");
   hvrinfo.push("<tr><td>Cell 25</td><td>Cell 26</td><td>Cell 27</td></tr>");
   hvrinfo.push("<tr><td>Cell 28</td><td>Cell 29</td><td>Cell 30</td></tr></table>");
   //Pushing the table end.

   //Information for the tooltips begin.
   hvrinfo.push("   <div class='tooltip00'>");
   hvrinfo.push("       1. RT conducts patient evaluation following <b id='redthis'>Eval & Treat Algorithm</b>.<br />");
   hvrinfo.push("       2. Level of Patients Asthma Control and current medications determined by RT.<br />");
   hvrinfo.push("       3. If Indicated, follow <b id='redthis'>Aerosolized medication Algorithm</b>.<br />");
   hvrinfo.push("       4. Plan constructed for therapy Pre-Op / Post-Op. If poorly controlled, advised or adminster step up in therapy (glucocorticoids).");
   hvrinfo.push("   </div>");
   //Information for the tooltips end.

   var hvrjoin = hvrinfo.join("");
   document.getElementById("data_here").innerHTML = hvrjoin;

tt_hvr1.js

 $(document).ready(function(){
    $(".hvrtip0").hover(function(){//Hover on the class. This works on all classes in the document.
        //mouse enters
            $(".tooltip00").css("display","block");
    },function(){
        //mouse leaves
            $(".tooltip00").css("display","block");
    });

    $(document).mousemove(function(event){
        var mx = event.pageX+15;
        var my = event.pageY+15;
        $(".tooltip00").css("left",mx+"px").css("top",my+"px");
    });
 });

【问题讨论】:

  • 我不认为这是这样做的方法。你有一个&lt;table&gt; 以及一个&lt;p&gt; 元素内的样式表……我也找不到类hvrtip0 的元素。
  • 我错过了什么?当你可以在你的主页中加载它们时,为什么还要用 css、html 和 JS 创建一个数组呢?
  • @Rob 当您在 JS 文件之前加载 jQuery 脚本时,它会按照 blgt 的建议工作。另外我想您应该将显示属性设置为“无”,以使工具提示在鼠标离开功能中不可见。
  • 感谢您在鼠标离开显示屏上发现这个错字。
  • @Ron (当你可以在你的主页中加载 css、html 和 JS 时,为什么还要麻烦创建一个数组?)我需要使用 push 方法,因为这就是我们的方式在工作时在电子病历EMR中显示数据。我能够按照您的建议显示它,但它不在 EMR 应用程序中。

标签: javascript jquery html css push


【解决方案1】:

交换这些行:

<script src='http://code.jquery.com/jquery-1.9.1.js' type="text/javascript" charset="utf-8"></script>
<script src='tt_hvr1.js' type="text/javascript" charset="utf-8"></script>

您需要在使用它的代码之前加载 jQuery。例如。注意这个小提琴是如何工作的:http://jsfiddle.net/uPASm/ 而这个不是:http://jsfiddle.net/uPASm/1/

【讨论】:

    【解决方案2】:

    你可以使用.html()函数来替换tooltip的内容,在hover函数中添加如下内容

     $(".tooltip00").html(this.innerHTML);
    

    你也需要改变

     $(".tooltip00").css("display", "block");
    

     $(".tooltip00").css("display", "none");
    

    隐藏工具提示。

    Demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多