【问题标题】:Add tooltip in a anchor link [duplicate]在锚链接中添加工具提示[重复]
【发布时间】:2015-12-14 23:03:34
【问题描述】:

我想在文本中添加工具提示,例如,如果我有这样的代码:

<a href="http://google.com" title="The World's Largest Search Engine!">Google!</a>

鼠标悬停时,我想显示该工具提示。 使用title 是一个好方法,但我怎样才能让它看起来更好呢?任何帮助: JSFIDDLE

【问题讨论】:

  • 如果你使用 jquery,你也可以使用 jquery-ui 库,除了其他很酷的东西还有一个工具提示功能:jqueryui.com/tooltip

标签: jquery html css stylesheet


【解决方案1】:

如果您希望使用 jquery 动态创建它,您可以执行以下操作:

<a href="http://google.com" data-title="The World's Largest Search Engine!">Google!</a>
<style>  
.box
{
  border: 1px solid green;
  position:absolute;
  color: white;
  top: 19px;
  left: 30px;
  background-color: black;
}
</style>
<script>
$().ready(function(){
$( "a" ).hover(
  function() {   
   var title = $(this).attr("data-title");  // extracts the title using the data-title attr applied to the 'a' tag
    $('<div/>', { // creates a dynamic div element on the fly
        text: title,
        class: 'box'
    }).appendTo(this);  // append to 'a' element
  }, function() {
    $(document).find("div.box").remove(); // on hover out, finds the dynamic element and removes it.
  }
);
  });
</script>

例如:http://jsfiddle.net/9RxLM/5164/

【讨论】:

  • 为什么使用data-title?只需使用 title 然后 enhance 它 - 获取要在样式提示中使用的文本,然后从元素中删除 title 属性,或者如 "duplicate question" 中的 an answer 建议的那样,使用 aria-label因为这是它的目的。
  • 您也可以这样做。我这样做是为了避免在删除元素的属性时涉及不必要的步骤。
猜你喜欢
  • 2019-01-04
  • 2022-10-23
  • 1970-01-01
  • 1970-01-01
  • 2011-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多