如何在jQuery中使用工具提示

How to use tooltip with jQuery?

本文关键字:工具提示 jQuery      更新时间:2023-09-26

我有一个简单的工具提示,在div中有很长的JavaScript代码。我想让它尽可能简单。

这是我的代码:

  <div onmouseover="document.getElementById('tt1DX1').style.display='block'" onmouseout="document.getElementById('tt1DX1').style.display='none'" style="position:relative;">Tool
    <div id="tt1DX1" class="toolTip_new pbc11_ttpos1_1Q_di" style="display: none;">
      <div class="tool_wrapper">
        <div class="tooltip_top_new"></div>
        <div class="tooltip_middle_new">
          <div class="content">
            <p>Please holder actuall text</p>
            <p>Please holder actuall text</p>
            </div>
          </div>
        <div class="tooltip_bot_new2"></div>
        </div>
      </div>
    </div>

css

.tooltip_top_new{
    background:url(../images/n_tooltip_top.png) no-repeat;
    height:9px;
    width:212px;
}
.tooltip_middle_new{
    background:url(../images/n_tooltip_middle.png) no-repeat;
    width:212px;
}
.tooltip_middle_new .content{
    padding:2px 13px 1px;
}
.tooltip_middle_new .content p{
    line-height: 1.3;
    margin-bottom: 1em;
    text-align: left;
}
.tooltip_bot_new2{
    background:url(../images/n_tooltip_bot2.png) no-repeat;
    height:21px;
    width:212px;
}

.Question_di{
    position:relative;
}
.pbc11_ttpos1_1Q_di {
    border: 0 solid #FF0000;
}
.toolTip_new {
    background: none repeat scroll 0 0 transparent;
    color: #5C5C5C;
    display: none;
    font: 10px/12px Tahoma,Geneva,sans-serif;
    left: -173px;
    top: -90px;
    position: absolute;
    z-index: 1000;
}

问题是,我必须复制&将onmouseover="document.getElementById('tt1DX1').style.display='block'" onmouseout="document.getElementById('tt1DX1').style.display='none'"粘贴到我使用工具提示的任何位置,我都希望避免它。

JQueryTools包含一个Tooltip模块,该模块将清除大量代码。

http://jquerytools.org/demos/tooltip/index.html

也可以在完全没有JavaScript的情况下创建工具提示,使用HTML和CSS如下:

<div class="has-tooltip">
  <button class="huge red">You Know You Wanna...</button>
  <div class="tooltip">Do NOT Press This Button.</div>
</div>

在CSS中:

.has-tooltip .tooltip {
  position: absolute;
  display: none;
  <style code to position (with margin-left and margin-top)
   and make the tooltip box look how you want>
}
.has-tooltip:hover .tooltip {
   display: block;
 }

谷歌"CSS工具提示"可以看到很多例子。